1、创建一个空列表,命名为names,往里面添加 Lihua、Rain、Jack、Xiuxiu、Peiqi和Black元素。
#!-*- coding:utf-8 -*- names = ["Lihua","Rain","Jack","Xiuxiu","Peiqi","Black"]
2、往(1)中的names列表里Black前面插入一个Blue。
#!-*- coding:utf-8 -*- names = ["Lihua","Rain","Jack","Xiuxiu","Peiqi","Black"] names.insert(-1,"Blue")
3、把names列表中Xiuxiu的名字改成中文。
#!-*- coding:utf-8 -*- names = ["Lihua","Rain","Jack","Xiuxiu","Peiqi","Black"] names.insert(-1,"Blue") names[names.index("Xiuxiu")] = "秀秀"
4、往names列表中Rain后面插入一个子列表["oldboy","oldgirl"]。
#!-*- coding:utf-8 -*- names = ["Lihua","Rain","Jack","Xiuxiu","Peiqi","Black"] names.insert(-1,"Blue") names[names.index("Xiuxiu")] = "秀秀" names.insert(2,["oldboy","oldgirl"]) print(names) # 验证
5、返回names列表中Peiqi的索引值(下标)。
#!-*- coding:utf-8 -*- names = ["Lihua","Rain","Jack","Xiuxiu","Peiqi","Black"] names.insert(-1,"Blue") names[names.index("Xiuxiu")] = "秀秀" names.insert(2,["oldboy","oldgirl"]) print(names.index("Peiqi"))
6、创建新列表[1,2,3,4,2,5,6,2,],合并到names列表中。
#!-*- coding:utf-8 -*- names = ["Lihua","Rain","Jack","Xiuxiu","Peiqi","Black"] names.insert(-1,"Blue") names[names.index("Xiuxiu")] = "秀秀" names.insert(2,["oldboy","oldgirl"]) print(names.index("Peiqi")) numbers = [1,2,3,4,2,5,6,2] names.extend(numbers) # extend()方法表示合并 print(names)
7、取出names列表中索引4-7的元素。
#!-*- coding:utf-8 -*- names = ["Lihua","Rain","Jack","Xiuxiu","Peiqi","Black"] names.insert(-1,"Blue") names[names.index("Xiuxiu")] = "秀秀" names.insert(2,["oldboy","oldgirl"]) print(names.index("Peiqi")) numbers = [1,2,3,4,2,5,6,2] names.extend(numbers) # extend()方法表示合并 print(names) print(names[4:8])
8、取出names列表中索引2-10的元素,步长为2。
#! -*-coding:utf-8 -*- names = ["Lihua","Rain","Jack","Xiuxiu","Peiqi","Black"] names.insert(-1,"Blue") # print(names) names[names.index("Xiuxiu")] = "秀秀" # print(names) names.insert(2,["oldboy","oldgirl"]) # print(names) print(names.index("Peiqi")) numbers = [1,2,3,4,2,5,6,2] names.extend(numbers) print(names) print(names[2:11:2]) # 列表切片“顾头不顾尾”,步长表示在指定范围间隔取值
9、取出names列表中最后3个元素。
#! -*-coding:utf-8 -*- names = ["Lihua","Rain","Jack","Xiuxiu","Peiqi","Black"] names.insert(-1,"Blue") # print(names) names[names.index("Xiuxiu")] = "秀秀" # print(names) names.insert(2,["oldboy","oldgirl"]) # print(names) print(names.index("Peiqi")) numbers = [1,2,3,4,2,5,6,2] names.extend(numbers) print(names) print(names[-3:]) # [-3:]表示取值范围为从列表的倒数第三个到末尾
10、循环names列表,打印每个元素的索引值和元素。
#!-*- coding:utf-8 -*- names = ["Lihua","Rain","Jack","Xiuxiu","Peiqi","Black"] names.insert(-1,"Blue") names[names.index("Xiouxiu")] = "秀秀" names.insert(2,["oldboy","oldgirl"]) print(names.index("Peiqi")) numbers = [1,2,3,4,2,5,6,2] names.extend(numbers) print(names) # 验证 #方法1 for i in names: print(names.index(i),i) # 方法2 enumerate()枚举 for index,i in enumerate(names): print(index,i)
11、循环names列表,打印每个元素的索引值和元素,当索引值为偶数时,把对应的元素改成-1。
#!-*- coding:utf-8 -*- names = ["Lihua","Rain","Jack","Xiuxiu","Peiqi","Black"] names.insert(-1,"Blue") names[names.index("Xiuxiu")] = "秀秀" names.insert(2,["oldboy","oldgirl"]) print(names.index("Peiqi")) numbers = [1,2,3,4,2,5,6,2] names.extend(numbers) print(names) # 验证 for index,i in enumerate(names): if index%2==0: names[index] = -1 print(index,i) print(names)
12、names列表里有3个2,请返回第二个2的索引值,不要人肉,要动态找。
#!-*- coding:utf-8 -*- names = ["Lihua","Rain","Jack","Xiuxiu","Peiqi","Black"] names.insert(-1,"Blue") names[names.index("Xiuxiu")] = "秀秀" names.insert(2,["oldboy","oldgirl"]) print(names.index("Peiqi")) numbers = [1,2,3,4,2,5,6,2] names.extend(numbers) print(names) # 验证 # 方法1 循环 count = 0 for index,i in enumerate(names): # print(index,i) if i == 2: count += 1 while count == 2: print(index) break else: continue # 方法2 print(names.index(2,names.index(2)+1))
13、现有商品列表如下:
products = [["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]],需打印出以下格式:
------ 商品列表 ------
0 iphone 6888
1 MacPro 14800
2 小米6 2499
3 Coffee 31
4 Book 60
5 Nike 699
参考答案如下:
#!-*- coding:utf-8 -*- products = [["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]] print("--------- 商品列表 --------") for index,i in enumerate(products): print("%s %s %s"%(index,i[0],i[1]))
14、根据(13)里的products列表写一个循环,不断询问用户想买什么,用户选择一个商品编号,就把对应的商品添加到购物车里,最终用户输入q退出时,打印购买的商品列表。
参考答案如下:
#!-*- coding:utf-8 -*- products = [["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]] shop_car = [] # 用户购物车 shop_cost = 0 # 用户花费的金额 exit_log = False # 标志位,默认设为False,退出 while not exit_log: print("------ 商品列表 ------") for index,i in enumerate(products): print("%s %s %s"%(index,i[0],i[1])) user_choice = input(" 输入你想购买的产品序号(按“q”退出):") if user_choice.isdigit(): # 判断用户输入的是否是数字 user_choice = int(user_choice) #强制转换为数字 if user_choice>=0 and user_choice < len(products): # 判断用户购买的商品是否在商品列表中 shop_car.append(products[user_choice]) # 加入购物车 shop_cost += products[user_choice][1] # 计算费用 print(" %s 已经加入你的购物车 "%products[user_choice]) else: print("抱歉,此商品不存在 ") elif user_choice == "q": # 用户选择退出 if len(shop_car)>0: # 判断用户是否购买了商品 print(" ------ 你的购物车 ------") for index,i in enumerate(shop_car): # index和i为临时变量,与前一个for循环里index和i作用的列表不同,故可重用 print("%s %s"%(i[0],i[1])) print(" 你此次购物的花费合计是:%s元 "%shop_cost) exit_log = True # 退出购物 else: exit_log = True # 未购买商品,不打印购物车商品,直接退出 else: # 输入不合法 exit_log = True