1、 循环names列表,打印每一个元素的索引值和元素,当索引值为偶数时,把对应的元素改为-1。git
思路: 能够用enumerate() # 枚举app
names = ['old_driver', 'rain', 'jack', 'shanshan', 'neo', 'black_girl', 1, 2, 3, 4, 2, 5, 6, 2] for i in enumerate(names): #enumerate用法 print(i) # 输出结果为: # (0, 'old_driver') # (1, 'rain') # (2, 'jack') # (3, 'shanshan') # (4, 'neo') # (5, 'black_girl') # (6, 1) # (7, 2) # (8, 3) # (9, 4) # (10, 2) # (11, 5) # (12, 6) # (13, 2)
上面的输出结果含有(), 若是不想带括号,利用下面的方式:iphone
names = ['old_driver', 'rain', 'jack', 'shanshan', 'neo', 'black_girl', 1, 2, 3, 4, 2, 5, 6, 2] for index,i in enumerate(names): #enumerate的另外一种用法 print(index,i) if index % 2 ==0: names[index] = -1 # 输出结果为: # 0 old_driver # 1 rain # 2 jack # 3 shanshan # 4 neo # 5 black_girl # 6 1 # 7 2 # 8 3 # 9 4 # 10 2 # 11 5 # 12 6 # 13 2 # 而且names也变成了 [-1, 'rain', -1, 'shanshan', -1, 'black_girl', -1, 2, -1, 4, -1, 5, -1, 2]
2、names里面有3个2,返回第二个2的索引值。(不要人肉数,要动态找,提示:找到第一个2的位置,在此基础上再找第二个)。spa
names = ['old_driver', 'rain', 'jack', 'shanshan', 'peiqi', 'black_girl', 1, 2, 3, 4, 2, 5, 6, 2] new_list = names[ names.index(2)+1:] #从“第一个2所在的索引值+1”日后截取一个新的列表,注意: 新列表中不能包含第一个2。 index_new_list = new_list.index(2) # 第二个2在新列表中的索引值 new_index = names.index(2) + index_new_list + 1 # 第二个2在原先names列表中的索引值 等于 第一个2的索引值 + 第二个2在新列表中的索引值 + 1 print(new_index) # 输出结果为 10
3、 现有商品列表以下:code
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
示例代码:blog
products = [ ['iphone8',6888], ['MacPro', 14800], ['小米6',2499], ['coffee',31],['book',80],['Nike shoes',799]] #初版: print('----------商品信息--------') for i in products: print(i) # 把products里面的内容依次打印 #输出结果: ----------商品信息-------- ['iphone8', 6888] ['MacPro', 14800] ['小米6', 2499] ['coffee', 31] ['book', 80] ['Nike shoes', 799] #第二版: 考虑打印出索引值,则应该利用enumerate products = [ ['iphone8',6888], ['MacPro', 14800], ['小米6',2499], ['coffee',31],['book',80],['Nike shoes',799]] print('----------商品信息--------') for index,info in enumerate(products): print(index,info) #输出结果: ----------商品信息-------- 0 ['iphone8', 6888] 1 ['MacPro', 14800] 2 ['小米6', 2499] 3 ['coffee', 31] 4 ['book', 80] 5 ['Nike shoes', 799] #第三版: 须要把每行的[ ]去掉,考虑到info表明的也是一个列表,能够用列表索引值取值的方法把info 这个小列表中的值拿出来。 (这一步须要注意) products = [ ['iphone8',6888], ['MacPro', 14800], ['小米6',2499], ['coffee',31],['book',80],['Nike shoes',799]] print('----------商品信息--------') for index,info in enumerate(products): print(index, info[0], info[1]) #输出结果: ----------商品信息-------- 0 iphone8 6888 1 MacPro 14800 2 小米6 2499 3 coffee 31 4 book 80 5 Nike shoes 799 #第四版:考虑输出信息的格式化 products = [ ['iphone8',6888], ['MacPro', 14800], ['小米6',2499], ['coffee',31],['book',80],['Nike shoes',799]] print('----------商品信息--------') for index,info in enumerate(products): print(' %s. %s %s' %(index, info[0], info[1]) ) #格式化输出 #输出结果: ----------商品信息-------- 0. iphone8 6888 1. MacPro 14800 2. 小米6 2499 3. coffee 31 4. book 80 5. Nike shoes 799
4、利用题三中的列表,写一个循环,不断的问用户想买什么,用户选择一个商品标号,就把对应的商品添加到购物车里,最终用户输入q退出时,打印购物车里的商品列表。索引
该示例用到的知识点:ip
exit_flag = False #标识符 a.isdigit() # 用于判断字符串变量a是否是整数的样子 len(list1) # 得出列表list1的长度
示例代码:字符串
products = [ ['iphone8',6888], ['MacPro', 14800], ['小米6',2499], ['coffee',31],['book',80],['Nike shoes',799]] cart = [] #定义一个购物车cart的空列表 exit_flag = False #标识符 while not exit_flag: #进入以后就打印“商品列表” print('----------商品列表----------') for index,i in enumerate(products): print(' %s. %s %s' %(index,i[0],i[1])) product_choice = input('请选择商品编号:') ''' 根据输入结果是否为数字来进行判断: 第一种状况:输入为数字 ''' if product_choice.isdigit(): #判断p字符串roduct_choice是否是整数的样子 product_choice = int(product_choice) #把字符串product_choice赋值成整数product_choice if product_choice >= 0 and product_choice < len(products): # 输入的数字在列表products索引值的范围以内 cart.append(products[product_choice]) #输入的索引值所对应的products元素添加到cart列表中 print('商品%s已被添加到购物车'%(products[product_choice][0])) #格式化输出 else: print('商品编号有误') #输入的数字不在products索引值范围内 #输入不为数字: elif product_choice =='q': if len(cart) >0: #购物车cart列表不为空 print('-------如下为您所选择的商品------') for index,i in enumerate(cart): #打印购物车列表信息 print(' %s. %s %s' %(index,i[0],i[1])) exit_flag = True #用于结束循环