python 字典和列表的问题


import math
import copy
warehouse = list()

warehouse.append({'name': 'Lolly Pop', 'price': 0.4, 'weight': 133})
warehouse.append({'name': 'Licorice', 'price': 0.1, 'weight': 251})
warehouse.append({'name': 'Chocolate', 'price': 1, 'weight': 601})
warehouse.append({'name': 'Sours', 'price': 0.01, 'weight': 513})
warehouse.append({'name': 'Hard candies', 'price': 0.3, 'weight': 433})

print('Source list of candies')
for item in warehouse:
    print(item)

b_warehouse=copy.deepcopy(warehouse)

for i in b_warehouse:
    
    if list(i.values())[2]>300:
        
      
        list(i.values())[1]=float(round(list(i.values())[1]-list(i.values())[1]*0.2,3))
        print(list(i.values())[1])#这里打印出来还是维持原价,不起变化
         
"""
output要求:
如果weight>300
price减去20%,也就是1变成0.8, 0.3变成0.24,问题看注释

直接改字典对象,而不是生成新数组修改这个数组的内容

import math
import copy
warehouse = list()
 
warehouse.append({'name': 'Lolly Pop', 'price': 0.4, 'weight': 133})
warehouse.append({'name': 'Licorice', 'price': 0.1, 'weight': 251})
warehouse.append({'name': 'Chocolate', 'price': 1, 'weight': 601})
warehouse.append({'name': 'Sours', 'price': 0.01, 'weight': 513})
warehouse.append({'name': 'Hard candies', 'price': 0.3, 'weight': 433})
 
print('Source list of candies')
for item in warehouse:
    print(item)

b_warehouse=copy.deepcopy(warehouse)

for i in b_warehouse:
    
    if i['weight']>300:
        
      
        i['price']=float(round(i['price']-i['price']*0.2,3))
        print(i['price'])

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632
可以查看手册:python- 字典 中的内容