列表用法:格式化商品列表、商品添加到購物車

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   #用於結束循環
相關文章
相關標籤/搜索