列表用法:格式化商品列表、商品添加到购物车

一、 循环names列表,打印每个元素的索引值和元素,当索引值为偶数时,把对应的元素改成-1。

思路: 可以用enumerate()   # 枚举

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)

上面的输出结果含有(), 如果不想带括号,利用下面的方式:

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]

 

二、names里面有3个2,返回第二个2的索引值。(不要人肉数,要动态找,提示:找到第一个2的位置,在此基础上再找第二个)。

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

 

 三、 现有商品列表如下:

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

示例代码:

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

 

四、利用题三中的列表,写一个循环,不断的问用户想买什么,用户选择一个商品标号,就把对应的商品添加到购物车里,最终用户输入q退出时,打印购物车里的商品列表。

该示例用到的知识点:

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   #用于结束循环

 

转载于:https://www.cnblogs.com/neozheng/p/8309479.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值