列表是一个数据的集合,集合内能够听任何数据类型,可对集合进行方便的增删改查操做
1. 定义列表:
方法一:
L1 = [] #定义空列表
L2 = ['A', 'B', 'C'] #存3个值,索引0-2
列表定义的值可重复
L5 = ['a', 'A', 'b', 'a', 'B']
names = [1, 2, 3, 4, 2, 5, 6, 2, 'old_dirver', 'rain', ['oldboy', 'oldgirl'], 'javk', ['李明'], 'peiqi', 'alex', 'black_girl'] for index,i in enumerate(names): #enumerate 枚举 if index % 2 == 0: names[index] = -1 print(index, i) print(names)
products = [['Iphone8', 6888],['MacPro', 14800],['小米6', 2499],['Coffee', 31],['Book', 80],['Nike Shoes', 799]] commodities = [] exit_flag = False #标识位 while not exit_flag: print('------------List of commodities------------') for index,i in enumerate(products): print("%s. %s %s" %(index, i[0], i[1])) choice = input('输入你想要的商品编号:') if choice.isdigit(): choice = int(choice) commodities.append(products[choice]) print('你选择了: ', products[choice]) # if choice >= 6: # print('The number of input must less than 6 !') # continue # elif choice < 0: # print("The number of input can't be negative !") # continue elif choice == 'q': print('========== 你选择的全部商品以下 ==========') for index,i in commodities: print('%s %s' %(index,i)) #break exit_flag = True