参考答案
>>> 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']
>>> 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') 1 >>> 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]]
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
0 old_driver 1 rain 2 ['oldboy', 'oldgirl'] 3 jack 4 姗姗 5 peiqi 6 alex 7 black_girl 8 [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) # 运行结果 0 old_driver 1 rain 2 jack 3 shanshan 4 peiqi 5 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 = [['Iphone8', 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)
product = [['Iphone8', 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)
购物车程序
# -*- coding:utf-8 -*- product = [['Iphone8', 6888], ['MacPro', 14800], ['小米6', 2499], ['Coffee', 31], ['Book', 80], ['Nike Shoes', 799]] shopping_cart = [] flag = False # 标志位 while not flag: print("----------商品列表 --------") for index, item in enumerate(product): msg = "%s. %s %s" % (index, item[0], item[-1]) print(msg) choice = input("输入你要买的商品编号|退出q :") if choice.isdigit(): choice = int(choice) if choice < len(product): shopping_cart.append(product[choice]) print('-----你购买了',product[choice]) else: print('你输入的商品不存在') elif choice == 'q': if len(shopping_cart) > 0: print("------你的购物车---------") for index, item in enumerate(shopping_cart): msg = "%s. %s %s" % (index, item[0], item[-1]) print(msg) flag = True # break else: print('你输入的有误,请重新输入')