列表練習題
1、創建一個空列表,命名為names,往里面添加old_driver,rain,jack,shanshan,peiqi,black_girl 元素
1 names = ['old_driver','rain','jack','shanshan','peiqi','black_girl'] 2 print(names)
2、往names列表里black_girl前面插入一個alex
1 names.insert(5,'alex') 2 print(names)
3、把shanshan的名字改成中文,姍姍
1 names[3] = '姍姍' 2 print(names)
4、往names列表里rain的后面插入一個子列表,[oldboy, oldgirl]
1 names.insert(2,'[oldboy,oldgirl]') 2 print(names)
5、返回peiqi的索引值
1 names.index('peiqi') 2 print(names.index('peiqi'))
6、創建新列表[1,2,3,4,2,5,6,2],合並入names列表
1 names1 = [1,2,3,4,2,5,6,2] 2 names = names+names1 3 print(names)
7、取出names列表中索引4-7的元素
print(names[4:7])
8、取出names列表中索引2-10的元素,步長為2
print(names[2:10:2])
9、取出names列表中最后3個元素
print(names[-3:])
10、循環names列表,打印每個元素的索引值,和元素
1 count = 0 2 for i in names: 3 print(count,i) 4 count += 1 5 6 #枚舉 7 # for i in enumerate(names): 8 # print(i) 9 for index,i in enumerate(names): 10 print(index, i)
11、循環names列表,打印每個元素的索引值,和元素,當索引值 為偶數時,把對應的元素改成-1
1 count = 0 2 for i in names: 3 if count % 2 == 0: 4 print(count,-1) 5 else: 6 print(count,i) 7 count += 1 8 9 #枚舉 10 for index, i in enumerate(names): 11 if index %2 == 0: #偶數 12 names[index] = -1 13 print(index, i) 14 print(names)
12、names里有3個2,請返回第2個2的索引值。不要人肉數,要動態找(提示,找到第一個2的位置,在此基礎上再找第2個)
1 #可以使用切片的方法,當找到第一個元素之后,從第一個元素后面切割出一個新的列表,然后在新的列表中找到第一個元素,然后用第一次找到的元素的索引加上第二次找到的元素的索引在加上1即可。 2 names = ['old_driver','rain','jack','shanshan','peiqi','black_girl',1,2,3,4,2,5,6,2] 3 first_index = names.index(2) 4 new_list = names[first_index+1:] 5 second_index = new_list.index(2) 6 second_val = names[first_index+second_index+1] 7 print(new_list,first_index,second_index) 8 print("second values", second_val)
13、現有商品列表如下:
# products = [ ['Iphone8',6888],['MacPro',14800], ['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799] ]
#
# 需打印出這樣的格式:
# ---------商品列表----------
# 0. Iphone8 6888
# 1. MacPro 14800
# 2. 小米6 2499
# 3. Coffee 31
# 4. Book 80
# 5. Nike Shoes 799
1 products = [ ['Iphone8',6888],['MacPro',14800], ['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799] ] 2 print("---------商品列表----------") 3 #枚舉 4 for index, i in enumerate(products): 5 print("%s %s %s"%(index,i[0],i[1]))
14、寫一個循環,不斷的問用戶想買什么,用戶選擇一個商品編號,就把對應的商品添加到購物車里, 最終用戶輸入q退出時,打印購物車里的商品列表。
1 exit_flag = False # 標志位,控制while循環 2 products = [['Iphone',6888],['MacPro',14800],['小米',2499],['Coffee',31],['Book',80],['NikeShoes',799]] 3 shopping_cart = [] # 創建購物車 4 while not exit_flag: 5 print("--------- 商品列表 ---------") 6 for index, i in enumerate(products): 7 print("%s. %s %s" %(index, i[0],i[1])) 8 choice = input("請問你想買什么:") 9 if choice.isdigit(): #isdigit 判斷是否為數字 10 choice = int(choice) #str轉換為int 11 if choice >=0 and choice < len(products): # 判斷輸入的數字是否超過列表的長度 12 print("%s. %s %s" %(index,i[0],i[1])) 13 shopping_cart.append(products[choice]) # 將選擇的商品加入購物車 14 else: 15 print("您輸出的數值有誤!") 16 elif choice == "q": 17 exit_flag = True #將while的標志位改變,跳出循環 18 if len(shopping_cart) > 0: # 判斷購物車是否有產品 19 print("--------- 您的購物車商品 ----------") 20 for index, i in enumerate(shopping_cart): 21 print("%s. %s %s" %(index,i[0],i[1])) 22 else: 23 print("您輸出的數值有誤!")