List Common actions:
1. Create two lists first
>>> Product = [' IPhone ', ' Xiaomi ', ' Meizu ']
>>> prices = [' 5000 ', ' 2000 ', ' 1500 ']
2. Print the list
>>> Product
[' IPhone ', ' Xiaomi ', ' Meizu ']
3. Append a single element
>>> product.append (' Samsung ')
>>> Product
[' IPhone ', ' Xiaomi ', ' Meizu ', ' Samsung ']
4. Number of occurrences of the statistic element in the list
>>> product.count (' Xiaomi ')
1
5. Find the element index location
>>> product.index (' Meizu ')
2
6. Inserting elements in the execution location
>>> Product.insert (2, ' ZTE ')
>>> Product
[' IPhone ', ' Xiaomi ', ' ZTE ', ' Meizu ', ' Samsung ']
7. Delete the execution location index element, without index, delete the last element by default
>>> Product.pop (2)
' ZTE '
>>> Product
[' IPhone ', ' Xiaomi ', ' Meizu ', ' Samsung ']
8. Remove the first element in the list for Samsung
>>> product.remove (' Samsung ')
>>> Product
[' IPhone ', ' Xiaomi ', ' Meizu ']
9. Ordering of list elements
>>> Product.sort ()
>>> Product
[' Meizu ', ' Xiaomi ', ' IPhone ']
>>> Product.reverse ()
>>> Product
[' IPhone ', ' Xiaomi ', ' Meizu ']
Through the above two lists, we can get the price of the goods, because their index position is corresponding.
For example:
>>> prices[product.index (' IPhone ')]
' 5000 '
This shows that the product information can be printed separately through a For loop:
>>> for I in product:
... print i,prices[product.index (i)]
...
IPhone 5000
Xiaomi 2000
Meizu 1500
In combination with the above list basic use, write down this example:
The user enters the salary amount, selects the purchase commodity (the amount not enough to buy the commodity, makes the prompt)
After the purchase of goods added to the shopping cart, the final output of what they bought
Create a Test text, or you can write it directly to the list:
# Cat Shop.txt
IPhone 5000
Xiaomi 2000
Meizu 1500
# vi shop_list.py#!/usr/bin/env python# coding:utf8import sysf = open (' Shop.txt ') product = []prices = []shop_list = []flag = 0 #标记flag2 = 0for line in f.readlines (): new_ Line = line.split () product.append (new_line[0]) # Loop appends the first index position value to the list product prices.append (int (new_line[1)) #循环第二个索引位置值追加列表prices #print product, ' \ n ',priceswhile true: for pp in product: &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF&NBSP;FLAG2&NBSP;!=&NBSP;1:PRINT&NBSP;PP, ' \ t ', Prices[product.index (PP)] #判断下面flag2变量值, whether to print product information while True: try: if flag == 1: #判断上次执行情况, if you have already done so, you are no longer prompted to enter payroll break else: salary = int (Raw _input (' Please enter your salary: ') break except Exception: print "Wages can only be entered in numbers!" " if salary < min (prices): #内置函数min () Judging the minimum value in the list print "Sorry, you can't afford to buy any goods!" " break choise_product = raw_ Input (' Please enter the name of the product you want to buy: '). Strip () #strip () function go to space if choise_product in product: product_prices = Prices[product.index (choise_product)] #通过输入的商品位置来找到商品价格 if salary >= product_prices: print "You have successfully purchased%s and added to your shopping cart." %choise_product shop_list.append ( CHOISE_PRODUCT) salary = salary - product_prices #工资减去现在商品的价格 if salary < min (prices): # Determine if the current remaining wage is less than the lowest price of the product print "Sorry, the remaining%d yuan, has not bought any goods!" " %salary print "Shopping Cart:%s" %shop_list sys.exit () else: print "You also have%d dollars left, you can also purchase the following items: " %salary for product_prices in prices: if product_prices <= salary: #打印剩余的钱数小于或等于列表的元素 print product[prices.index (product_prices)], ' \ t ',product_prices flag = 1 # Used to determine whether to execute the above command, no longer the next prompt to enter wages. The following flags are the same flag2 = 1 #用于判断是否执行上面命令, if executed, will no longer print product information else: print "Your wages can't afford to buy%s! Please re-select the item: " %choise_product flag = 1 else: print ' \ 033[31;1m do not have the goods you want! please re-select: \033[0m ' flag = 1 flag2 = 2 #非1都可以. If it is equal to 1, the second input of the purchased product name is wrong, the product information will not be printed, because the FLAG2 variable has been assigned a value of 1
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/7E/wKioL1X_sK6TokncAAQRNiCpqLU679.jpg "title=" List.png "alt=" Wkiol1x_sk6tokncaaqrnicpqlu679.jpg "/>
This article is from the "Li Zhenliang Technology Blog" blog, make sure to keep this source http://lizhenliang.blog.51cto.com/7876557/1696772
Python list (list) practice