Python第三天作业操作答案
vegetable_price = {
'紫贝天葵': {'low':6.50, 'mean':6.75, 'high':7.00},
'金丝南瓜': {'low':7.50, 'mean':7.75, 'high':8.00},
'鱼腥草叶': {'low':8.00, 'mean':8.50, 'high':9.00},
'鱼腥草根':{'low':6.60, 'mean':6.75, 'high':7.00},
'圣女果': {'low':3.50, 'mean':3.75, 'high':4.00},
'荷兰豆': {'low':8.00, 'mean':8.50, 'high':9.00},
'朝天椒': {'low':18.00, 'mean':19.00, 'high':20.00},
'绿菜花': {'low':1.80, 'mean':1.90, 'high':2.00},
'球茎茴香': {'low':7.50, 'mean':7.75, 'high':8.00},
}
题目:
1. 从字典中提取出朝天椒的今日最高价,查找出鱼腥草叶的均价
2. 查找数据中有没有红尖椒存在,如果没有则返回“没有这种菜”,同时在字典中添加这种菜,菜的价格为:5.00, 5.25,5.50,然后打印新的字典;如果有就返回这种菜的全部价格。
3. 弹出鱼腥草叶的数据
4. 求出所有蔬菜最高值的平均值
============================================================================================================
操作代码:
1. 从字典中提取出朝天椒的今日最高价,查找出鱼腥草叶的均价
Vegetable_price[‘朝天椒’][‘high’]
Vegetable_price[‘鱼腥草叶’][‘mean’]
或者:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2. 查找数据中有没有红尖椒存在,如果没有则返回“没有这种菜”,同时在字典中添加这种菜,菜的价格为:5.00, 5.25,5.50,然后打印新的字典;如果有就返回这种菜的全部价格。
if vegetable_price.get('红尖椒', "没有这种菜") == "没有这种菜":
vegetable_price['红尖椒'] = {'low':5.00, 'mean':5.25, 'high':5.50}
print(vegetable_price)
else:
print(vegetable_price.get('红尖椒', "没有这种菜"))
或者:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3. 弹出鱼腥草叶的数据
vegetable_price.pop("鱼腥草叶")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4. 求出所有蔬菜最高值的平均值
num = 0
# 求出所有蔬菜最高值的平均值
for key in vegetable_price:
num += vegetable_price[key]['high']
high_mean = num / len(vegetable_price)
high_mean
或者:
============================================================================================================








暂无数据