2 -4 -1 list列表练习解答
Posted mobai-c
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2 -4 -1 list列表练习解答相关的知识,希望对你有一定的参考价值。
答案
1
>>> 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‘]
2
>>> names.index(‘black_girl‘) 5 >>> names.insert(5,‘alex‘) >>> names [‘old_driver‘, ‘rain‘, ‘jack‘, ‘shanshan‘, ‘peiqi‘, ‘alex‘, ‘black_girl‘]
3
>>> names[names.index(‘shanshan‘)] = ‘姗姗‘ >>> names [‘old_driver‘, ‘rain‘, ‘jack‘, ‘姗姗‘, ‘peiqi‘, ‘alex‘, ‘black_girl‘]
4
>>> names2 [‘oldboy‘, ‘oldgirl‘] >>> names.index(‘rain‘) >>> names.insert(2,names2) >>> names [‘old_driver‘, ‘rain‘, [‘oldboy‘, ‘oldgirl‘], ‘jack‘, ‘姗姗‘, ‘peiqi‘, ‘alex‘, ‘black_girl‘]
5
>>> names.index(‘peiqi‘) 5
6
>>> 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]]
7-9
>>> 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]]
10
>>> 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
11
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)
12
13
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
以上是关于2 -4 -1 list列表练习解答的主要内容,如果未能解决你的问题,请参考以下文章