用 for 循环去除字典 fruit _ price ={"苹果":8.5,"橘
子":5.8,"香蕉":4.9,"榴莲":30,"菠萝":5.2}中价格大于
10的元素,得到新的字典 new _ fruit _ price 。

for循环去除字典内元素,一直int str报错
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- 於黾 2023-04-19 09:18关注
fruit_price ={"苹果":8.5,"橘子":5.8,"香蕉":4.9,"榴莲":30,"菠萝":5.2} new_fruit_price=fruit_price.copy() #先得到一个copy for k,v in fruit_price.items(): if v > 10 : del new_fruit_price[k] #把不要的删掉 print(new_fruit_price)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用