First, List
Lists and dictionaries are two of the most common data types
1. Requirements: How to store the class more than 80 people's name, how to achieve?
names = ["Zhangyang", "Guyun", "Xiangpeng", "Xuliangchen"]print (names[0],names[2]) #取出 Zhangyang xiangpengprint (names[ 2:4]) #切片, remove "Xiangpeng", "Xuliangchen" and output print (Names[0:4]) #切片, remove "Zhangyang", "Guyun", "Xiangpeng", "Xuliangchen"] Print (Names[-1]) #切片, negative numbers refer back to print (names[-2:]) #取最后2个值
Note: The front and back 0 can be omitted
2. Examples of various operations of the list
names = "Zhangyang Guyun xiangpeng xuliangchen" #以此列表为例
Increase:
Names.append ("Leihaidong") #在列表最后增加
Insert:
Names.insert (1, "Chenronghua") #位置1, insert Data Names.insert (3, "Xinzhiyu") #位置3, insert data
Modify:
NAMES[2] = "Xiedi" #下标位置2, replaced with "Xiedi"
Delete: (Three ways)
Names.remove ("Chenronghua") #删除列表中指定数据del names[1] #删除下标位置1的数据names. Pop () #不加下标, delete the last data, add subscript, and Del effect is the same
Copy:
(1)
names = ["Zhangyang", "Guyun", "Xiangpeng", "xuliangchen"]names2 = Names.copy () names[2] = "Peng" Print (names) print (Names2)
(2)
A.
names = ["Zhangyang", "Guyun", "Xiangpeng", ["Alex", "Jack"], "xuliangchen"]names2 = Names.copy () #浅copy, copy only the first layer names[2] = "To Peng" names[3][0] = "ALEX" Print (names) print (Names2)
View the output, Names2, Xiangpeng is not changed, the data in the sub-list is modified. Because the data in the child list is just a memory address, copy only the memory address
B. Shallow copy, The purpose of shallow copy: Create a federated account
Import copynames = ["Zhangyang", "Guyun", "Xiangpeng", ["Alex", "Jack"], "xuliangchen"]names2 = copy.copy (names) print ( Names) print (names2) names[2] = "to Peng" names[3][0] = "ALEX" Print (names) print (Names2)
(3) deep copy
Import copynames = ["Zhangyang", "Guyun", "Xiangpeng", ["Alex", "Jack"], "xuliangchen"]names2 = copy.deepcopy (names) Print (names) print (names2) names[2] = "to Peng" names[3][0] = "ALEX" Print (names) print (Names2)
At this point,names and Names2 will be two separate data.
List loop:
For i in Names: print (i)
List slices:
Print (Names[0:-1:2]) print (Name[::2])
The above two commands have the same effect, and the data in the output list is spaced out. 0 and 1 in the list operation can be omitted.
Two, the meta-group
Tuples are actually similar to the list, but also to save a group of numbers, but once it is created, it can no longer be modified, so it is called a read-only list.
Grammar:
Names = (' Alex ', ' Jack ')
Tuples have only 2 methods, one is count, the other is index
Third, the procedure practice
Program: Shopping Cart Program
Demand:
- After you start the program, let the user enter the payroll, and then print the list of items
- Allow users to purchase items based on their product number
- After the user selects the product, checks whether the balance is enough, enough on the direct debit, enough to remind
- You can exit at any time to print the purchased goods and balances when exiting
The code is as follows:
Shopping_list = [] #已购买的商品列表, pre-defined empty list salary = input ("Please input your saraly:") # After starting the program, let the user enter payroll product_list = [(' Iphon E ', 5800), (' bike ', +), (' coffee ', +), (' Macbook Pro ', 12000), (' Alex Python ', Bayi), (' A ', ')]if Salary.isdig It (): salary = Int (salary) while True:for Index,item in Enumerate (product_list): #从商品列表中取数据和下标 # Print (Product_list.index (item), item) print (index,item) User_choice = input ("Select the item you want to buy?") >>>: ") If User_choice.isdigit (): #如果输入的是数字 user_choice = Int (user_choice) #转数据类型 if User_choice < Len (product_list) and User_choice >= 0: #如果输入的商品序列号在列表下标范围内, enter the shopping process P_item = Product_lis T[user_choice] If p_item[1] <= salary: #买得起 shopping_list.append (P_item) Salary-= p_item[1] Print ("Added%s into shopping cart,your current balance is \033[31;1m%s\033[ 0m "% (p_item,salary)) Else: Print ("\033[41;1m your balance is left [%s], can't buy this item \033[0m"%salary) else:print ("Product Code [%s] is not exist!!! "%user_choice) elif User_choice = = ' Q ': print ("---------shopping list---------") For P in Shopping_list:print (p) print ("Your Current balance:", salary) ex It () else:print ("Invalid option")
Python Learning Notes (lists, tuples, shopping cart instances)