列表是一個數據的集合,集合內能夠聽任何數據類型,可對集合進行方便的增刪改查操做
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