python - 如何从python中没有键的字典中读取?
问题描述
这是我之前的问题的后续,我正在处理多个字典并遇到问题。我目前有一个存储数据的主字典和一个添加数据的空白字典。
Dairy_goods = {1:{'item':'Milk','p':2.47,'g':0.16,'offer':'Yes'},
2:{'item':'Butter','p':4.50,'g':0.32,'offer':'No'},
3:{'item':'Egg','p':3.40,'g':0.24,'offer':'No'}}
shopping_basket={}
例如,我想将“牛奶”项目添加到空白购物篮中,我会按照以下方式进行。
choose=int(input('1.Item= Milk, Price= $2.47, GST= $0.16, Offer=Yes\n'
'2.item= Butter, Price= $4.50, GST= $0.32, Offer=No\n'
'3.Item= Egg, Price= $4.50, GST= $0.32, Ofer=No\n'
'Enter your option: '))
qnty=int(input('How many do you want?: '))
shopping_basket[Dairy_goods[choose]['item']] = shopping_basket.get(Dairy_goods[choose]['item'], 0) + qnty,Dairy_goods[choose]['p'],Dairy_goods[choose]['g'],Dairy_goods[choose]['offer']
我会得到如下输出。
print(shopping_basket)
{'Milk': (2, 2.47, 0.16, 'Yes')}
现在我想编辑这个新字典中的值,我该怎么做呢?因为它没有修复密钥?我知道它在一个元组中并且可以转换为一个列表,但仍然会给出一个错误,如下所示。
for item in shopping_basket:
item = str(input('key in an item to edit: '))
for item in shopping_basket:
shopping=list(shopping_basket)
qnty = int(input('Key in the quantity of %s you want: ' % item))
shopping[item][0] = qnty # i would get a error here.
print(shopping)
让我知道是否需要更多说明,提前谢谢你。
解决方案
There are a few things off in your code in terms of data structures. First, a dictionary with incrementing keys {1: ..., 2: ..., ...}
is basically the same as a list, so you can just as well use the simpler data structure and select items by indexing. The only thing to note here is that list indices start at 0
, so you need to subtract 1
from your user's choice.
Dairy_goods = [{'item':'Milk','p':2.47,'g':0.16,'offer':'Yes'},
{'item':'Butter','p':4.50,'g':0.32,'offer':'No'},
{'item':'Egg','p':3.40,'g':0.24,'offer':'No'}]
choose = int(input('1.Item = ... ')) - 1 # subtract 1 to get 0-indexing
qnty = int(input('How many do you want?: '))
Next up, there is a lot going on in the line shopping_basket[Dairy_goods[choose]['item']] = ...
, which makes it hard to work with. In my comment, I suggested to go for dictionaries to store the items in the shopping basket to make them easier to modify. The new format of the basket would look like
# shopping_basket
{'Milk':{'item':'Milk','p':2.47,'g':0.16,'offer':'Yes', 'qnty':1}}
which is a bit redundant because of the double item name (as key and within values), which is normally considered a bad thing, but in this case it allows for a much easier access to the shopping cart items.
However, it's actually sufficient to store only the quantities in the shopping basket - {'Milk':1, 'Egg':2, ...}
- because all the other information is in Dairy_goods
.
selection = Dairy_goods[choose] # e.g., {'item':'Milk','p':2.47,'g':0.16,'offer':'Yes'}
item = selection['item'] # e.g., 'Milk'
shopping_basket[item] = shopping_basket.get(item, 0) + qnty
# optional: delete item if the quantity is <= 0
if shopping_basket[item]['qnty'] <= 0:
shopping_basket.pop(item)
Then, the edit part becomes:
item = input('key in an item to edit: ') # e.g., 'Milk'
qnty = int(input(f'Key in the quantity of {item} you want: ')) # did you hear about f-strings? Super useful!
shopping_basket[item] = qnty # set the new quantity
推荐阅读
- c# - 检查电子邮件是否存在,元素未找到功能
- javascript - 如何限制 Firestore 安全规则中请求的文档数量?
- python - 没有背景 tkinter 的标签
- excel - 创建文件夹和子文件夹
- reactjs - React Navigation:嵌套堆栈和选项卡导航器
- wordpress - Wordpress - 停用帖子的单页
- unix - 编写一个循环进入子目录文件夹,然后在 unix 中复制和重新标记文件夹名称
- python-3.x - Itertool.product at to 字符串
- javascript - 模板不会在没有刷新的情况下上传到侧边栏(React)
- .htaccess - 将所有页面从站点 A 重定向到站点 B,包括 http 和 https