标签:repr 技术 运行 nis iphone size product format col
>>> names = [] >>> names.append(‘old_driver‘) >>> names.append(‘rain‘) >>> names.append(‘jack‘) >>> names.append(‘shanshan‘) >>> names.append(‘peiqi‘) >>> names.append(‘black_girl‘) >>> names [‘old_driver‘, ‘rain‘, ‘jack‘, ‘shanshan‘, ‘peiqi‘, ‘black_girl‘]
#list方法 >>> dir(names) [‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__ ‘, ‘__hash__‘, ‘__iadd__‘, ‘__imul__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘append‘, ‘clear‘, ‘copy‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘ pop‘, ‘remove‘, ‘reverse‘, ‘sort‘]
>>> names.index(‘black_girl‘) 5 >>> names.insert(5,‘alex‘) >>> names [‘old_driver‘, ‘rain‘, ‘jack‘, ‘shanshan‘, ‘peiqi‘, ‘alex‘, ‘black_girl‘]
>>> names[names.index(‘shanshan‘)] = ‘姗姗‘ >>> names [‘old_driver‘, ‘rain‘, ‘jack‘, ‘姗姗‘, ‘peiqi‘, ‘alex‘, ‘black_girl‘]
>>> names2 [‘oldboy‘, ‘oldgirl‘] >>> names.index(‘rain‘) >>> names.insert(2,names2) >>> names [‘old_driver‘, ‘rain‘, [‘oldboy‘, ‘oldgirl‘], ‘jack‘, ‘姗姗‘, ‘peiqi‘, ‘alex‘, ‘black_girl‘]
>>> names.index(‘peiqi‘) 5
>>> names3 = [1,2,3,4,2,5,6,2] >>> names.extend(names3) >>> names [‘old_driver‘, ‘rain‘, [‘oldboy‘, ‘oldgirl‘], ‘jack‘, ‘姗姗‘, ‘peiqi‘, ‘alex‘, ‘black_girl‘, 1, 2, 3, 4, 2, 5, 6, 2] >>> names4 = names + names3 >>> names4 [‘old_driver‘, ‘rain‘, [‘oldboy‘, ‘oldgirl‘], ‘jack‘, ‘姗姗‘, ‘peiqi‘, ‘alex‘, ‘black_girl‘, 1, 2, 3, 4, 2, 5, 6, 2] >>> names.append(names3) >>> names [‘old_driver‘, ‘rain‘, [‘oldboy‘, ‘oldgirl‘], ‘jack‘, ‘姗姗‘, ‘peiqi‘, ‘alex‘, ‘black_girl‘, [1, 2, 3, 4, 2, 5, 6, 2]]
>>> names[4:8] [‘姗姗‘, ‘peiqi‘, ‘alex‘, ‘black_girl‘] >>> >>> names[2:11:2] [[‘oldboy‘, ‘oldgirl‘], ‘姗姗‘, ‘alex‘, [1, 2, 3, 4, 2, 5, 6, 2]] >>> >>> names[-3:] [‘alex‘, ‘black_girl‘, [1, 2, 3, 4, 2, 5, 6, 2]]
>>> for k in names: ... print(names.index(k),k)
names3 = [‘old_driver‘, ‘rain‘, ‘jack‘, ‘shanshan‘, ‘peiqi‘, ‘black_girl‘] count = 0 for i in names3: print(count,i) count += 1 #运行结果 old_driver rain [‘oldboy‘, ‘oldgirl‘] jack 姗姗 peiqi alex black_girl [1, 2, 3, 4, 2, 5, 6, 2]
for i in enumerate(names3): #枚举 print(i) # 运行结果 (0, ‘old_driver‘) (1, ‘rain‘) (2, ‘jack‘) (3, ‘shanshan‘) (4, ‘peiqi‘) (5, ‘black_girl‘)
for index,item in enumerate(names3): #枚举 print(index,item) # 运行结果 old_driver rain jack shanshan peiqi black_girl
names = [‘old_driver‘, ‘rain‘, [‘oldboy‘, ‘oldgirl‘], ‘jack‘, ‘姗姗‘, ‘peiqi‘, ‘alex‘, ‘black_girl‘, [1, 2, 3, 4, 2, 5, 6, 2]] for k in names: index = names.index(k) if index%2 == 0: names[index] = -1 print(index, k) print(names)
names3 = [‘old_driver‘, ‘rain‘, ‘jack‘, ‘shanshan‘, ‘peiqi‘, ‘black_girl‘] for index,item in enumerate(names3): if index%2 == 0: print(index,item) names3[index] = -1 print(names3)
product = [[‘iphone‘,6888],[‘macpro‘,14800],[‘小米6‘,2499],[‘coffee‘,31],[‘book‘,80],[‘nike shoes‘,799]] print("-----商品列表-----") for index,item in enumerate(product): msg = "%s %s %s"%(index,product[index][0],product[index][-1]) print(msg) #运行结果 -----商品列表----- 0 iphone 6888 1 macpro 14800 2 小米6 2499 3 coffee 31 4 book 80 5 nike shoes 799 Process finished with exit code 0
另一种
product = [[‘iphone‘,6888],[‘macpro‘,14800],[‘小米6‘,2499],[‘coffee‘,31],[‘book‘,80],[‘nike shoes‘,799]] print("-----商品列表-----") for index,item in enumerate(product): msg = "%s %s %s"%(index,item[0],item[-1]) print(msg) #运行结果 -----商品列表----- 0 iphone 6888 1 macpro 14800 2 小米6 2499 3 coffee 31 4 book 80 5 nike shoes 799 Process finished with exit code 0
product = [[‘iphone‘,6888],[‘macpro‘,14800],[‘小米6‘,2499],[‘coffee‘,31],[‘book‘,80],[‘nike shoes‘,799]] shopping_cart = [] while True: print("------商品列表------") for index,i in enumerate(product): print("%s %s %s"%(index,i[0],i[1])) choose = input("输入商品编号(0-5),退出(q)") if choose.isdigit(): choose =int(choose) if choose < len(product): shopping_cart.append(product[choose]) print("商品已加入购物车!%s" %(product[choose])) else: print("输入有误,商品不存在") elif choose == ‘q‘: if len(shopping_cart) > 0: print("-----你的购物车------") for index ,p in enumerate(shopping_cart): print("%s %s %s "%(index,p[0],p[1])) break else: print("输入有误,请重新输入") #运行结果 输入商品编号(0-5),退出(q)2 商品已加入购物车![‘小米6‘, 2499] ------商品列表------ 0 iphone 6888 1 macpro 14800 2 小米6 2499 3 coffee 31 4 book 80 5 nike shoes 799 输入商品编号(0-5),退出(q)q -----你的购物车------ 0 macpro 14800 1 小米6 2499 Process finished with exit code 0
标签:repr 技术 运行 nis iphone size product format col
原文地址:https://www.cnblogs.com/Mobai-c/p/10120451.html