1.创建一个空列表,命名为names,往里面添加old_driver,rain,jack,shanshan,peiqi,black_girl
1 >>> names = [] 2 >>> names.append('old_driver') 3 >>> names.append('rain') 4 >>> names.append('jack') 5 >>> names.append('shanshan') 6 >>> names.append('black_girl') 7 >>> names 8 ['old_driver', 'rain', 'jack', 'shanshan', 'black_girl']
2.往names列表里black_girl前面插入一个alex
1 >>> names.insert(4,'alex') 2 >>> names 3 ['old_driver', 'rain', 'jack', 'shanshan', 'alex', 'black_girl']
3.把shanshan列表里的名字改成中文,姗姗
1 >>> names[3] = '姗姗' # 通过赋值修改 2 >>> names 3 ['old_driver', 'rain', 'jack', '姗姗', 'alex', 'black_girl']
4.往names列表里rain的后面插入一个子列表,['oldboy','oldgirl']
1 >>> names.insert(2,['oldboy','oldgirl']) 2 >>> names 3 ['old_driver', 'rain', ['oldboy', 'oldgirl'], 'jack', '姗姗', 'alex', 'black_girl']
5.返回alex的索引值
1 >>> names.index('alex') 2 5
6.创建新列表{1,2,3,4,2,5,6,2},合并入names列表
1 >>> new_list=[1,2,3,4,2,5,6,2] 2 >>> new_list 3 [1, 2, 3, 4, 2, 5, 6, 2] 4 >>> names.extend(new_list) 5 >>> names 6 ['old_driver', 'rain', ['oldboy', 'oldgirl'], 'jack', '姗姗', 'alex', 'black_girl', 1, 2, 3, 4, 2, 5, 6, 2]
7.取出names列表中索引4-7的元素
1 >>> names[4:7] 2 ['姗姗', 'alex', 'black_girl']
8.取出names列表中索引2-10的元素,步长为2
1 >>> names[2:10:2] 2 [['oldboy', 'oldgirl'], '姗姗', 'black_girl', 2]
9.取出names列表中最后三个元素
1 >>> names[-3:] 2 [5, 6, 2]
10.循环names列表,打印每个元素的索引值,和元素
1 names = ['old_driver', 'rain', ['oldboy', 'oldgirl'], 'jack', '姗姗', 'alex', 'black_girl', 1, 2, 3, 4, 2, 5, 6, 2] 2 index = 0 3 for i in names: 4 print(index, i) 5 index += 1
11.循环names列表,打印每个元素的索引值,和元素,当索引为偶数时,把对应的元素改成-1
1 names = ['old_driver', 'rain', ['oldboy', 'oldgirl'], 'jack', '姗姗', 'alex', 'black_girl', 1, 2, 3, 4, 2, 5, 6, 2] 2 index = 0 3 for i in names: 4 if index % 2 == 0: 5 names[index] = -1 # 索引对应的元素替换成-1 6 index += 1 7 print(names)
12.names里有3个2,请返回第2个2的索引值,不要人肉数,动态找(提示,找到第一个2的位置,在此基础上再找第2个)
1 names = [1, 56, 2, 5, 6, 7, 8, 2, 4, 5, 2] 2 first_index = names.index(2) # 通过索引找到2所在的位置 3 new_list = names[first_index+1:] # 切片时需要加1,因为不加1是包含2的 4 second_index = new_list.index(2) # 索引是从0开始的 5 second_val = names[first_index + second_index + 1] 6 print(first_index, new_list, second_index) 7 print(second_val)
13.现有商品列表如下:
products = [['Iphone8', 6666], ['MacPro', 8888], ['小米 ', 9999], ['Coffee', 9999], ['Book', 9888]]
需要打印出这样的格式:
--------商品列表--------
0.Iphone8 6666
1.MacPro 8888
2.小米 9999
3.Coffee 9999
4.Book 9888
1 products = [['Iphone8', 6666], ['MacPro', 8888], ['小米 ', 9999], ['Coffee', 9999], ['Book', 9888]] 2 out = '商品列表' 3 print(out.center(20, '-')) 4 for index, i in enumerate(products): 5 print("%s.%s %d" % (index, i[0], i[1]))
14.写一个循环,不断的问用户想买什么,用户选择一个商品编号,就把对应的商品添加到购物车里,最终用户输入q退出时,打印购物车里的商品列表
1 exit_flog = False # 标志位 2 shopping_cat = [] 3 products = [['Iphone8', 6666], ['MacPro', 8888], ['小米 ', 9999], ['Coffee', 9999], ['Book', 9888]] 4 out = '商品列表' 5 # 循环打印商品列表 6 print(out.center(20, '-')) 7 for index, i in enumerate(products): 8 print("%s.%s %s" % (index, i[0], i[1])) 9 while not exit_flog: 10 choice = input("你想买什么呢:") 11 # 判断输入字符是否由数字组成 12 if choice.isdigit(): 13 choice = int(choice) 14 # 推荐使用连锁比较 15 if 0 <= choice < len(products): 16 shopping_cat.append(products[choice]) 17 print("添加到购物车中%s" % (products[choice])) 18 else: 19 print("商品编号不存在") 20 # 打印购物车商品列表 21 elif choice == 'q': 22 if len(shopping_cat) > 0: 23 for index, i in enumerate(shopping_cat): 24 print("%s.%s %s" % (index, i[0], i[1])) 25 # break 26 exit_flog = True