列表练习题
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("您输出的数值有误!")