通过了解value-python字典增加一个键

通过了解value-python字典增加一个键,python,Python,我需要编写一个代码,每当有人购买一个产品,需求字典中的键就会增加1。 我不知道如何通过知道值来增加字典中的键 这就是我所尝试的: demand = {"bread":0,"butter":0,"cheese":0, "water":0,"ice cream":0} # bread is the value and 0 is the key, which I want to increase every time def bill(buy_lst): for item in buy_ls

我需要编写一个代码,每当有人购买一个产品,需求字典中的键就会增加1。 我不知道如何通过知道值来增加字典中的键

这就是我所尝试的:

demand = {"bread":0,"butter":0,"cheese":0, "water":0,"ice cream":0}
# bread is the value and 0 is the key, which I want to increase every time

def bill(buy_lst):
    for item in buy_lst:
        demand[demand.get(item)] += 1
当我运行时,它会说:

demand[demand.get(item)] += 1
KeyError: 0

谢谢大家!

您似乎误解了
,在您的情况下,字符串是
,它们的数量是

demand = {"bread": 0, "butter": 0, "cheese": 0, "water": 0, "ice cream": 0}
所以,您想要的是增加给定键的值

buy_lst[item] += 1

要使用它,你需要向用户询问一种产品,比如

def bill(buy_lst):
    item = None
    while not item:
        item = input("Please select in " + str(list(buy_lst.keys())))
        item = item if item in buy_lst else None
    buy_lst[item] += 1


if __name__ == '__main__':
    demand = {"bread": 0, "butter": 0, "cheese": 0, "water": 0, "ice cream": 0}
    bill(demand)
    print(demand)

你的问题是如何添加到字典中。请注意以下事项:

demand={“面包”:0,“黄油”:0,“奶酪”:0,“水”:0,“冰淇淋”:0}
购买_lst=[“面包”、“黄油”、“面包”]
def账单(购买):
对于购买列表中的项目:
打印(按需获取(项目))
#需求[需求.获取(项目)]+=1
账单(购买)
这将产生:

0
0
0
换句话说,在你的

demand[demand.get(item)]+=1
你正在做:

需求[0]+=1
将返回错误:

Traceback (most recent call last):
  File "/Users/felipefaria/Desktop/test/main.py", line 11, in <module>
    bill(buy_lst)
  File "/Users/felipefaria/Desktop/test/main.py", line 8, in bill
    demand[demand.get(item)] += 1
KeyError: 0
这将输出:

{'bread': 2, 'butter': 1, 'cheese': 0, 'water': 0, 'ice cream': 0}

我想你的意思是“增加我知道的钥匙的价值”。因为只有值是整数,所以只有值可以递增。所以递增的值应该是:

demand={“面包”:0,“黄油”:0,“奶酪”:0,“水”:0,“冰淇淋”:0}
def账单(购买):
对于购买列表中的项目:
需求[项目]+=1

为了澄清,字典项的第一部分是
,第二部分是

,您可能会混淆键和值。在
需求中
,键是项目,值是计数

#!/usr/bin/env python

demand = {"bread":0, "butter":0, "cheese":0, "water":0, "ice cream":0}
def bill(buy_list):
    for item in buy_list:
        demand[item] += 1

buy_list = ["bread", "water"]

print("Before billing:")
print(demand)

bill(buy_list)

print("After billing")
print(demand)
这张照片是:

Before billing:
{'bread': 0, 'butter': 0, 'cheese': 0, 'water': 0, 'ice cream': 0}
After billing
{'bread': 1, 'butter': 0, 'cheese': 0, 'water': 1, 'ice cream': 0}

你的问题很简单。您只需在函数参数
buy_lst
前面添加操作符
*
,就可以使用
*buylst
。请参阅以下代码:

demand = {"bread":0,"butter":0,"cheese":0, "water":0,"ice cream":0}
# bread is the value and 0 is the key, which I want to increase every time

def bill(*buy_lst):  # see this line
    for item in buy_lst:
        demand[item] += 1  # see this line

bill("bread", "cheese") # The client buys the products `bread` and `cheese`
print(demand)
bill("bread", "cheese", "water", "butter")
print(demand)
输出


bread
是键,
0
是值。您想增加
0
,对吗?您没有运行函数
def bill()
。还有另一种方法可以实现您的目标。请参阅下面的代码!谢谢你完整而详细的回答:)我明天有个测试,你真的帮了我!!我强烈建议大家多看看字典,阅读它们的官方文档。我想只要你看一看,很多东西都会点击谢谢,我去看看!此外,请确保在您认为最有用的答案旁边按复选标记,以便您的问题对社区的其他人标记为“已回答”@公主(不客气;)选择一个答案并接受它;)操作符*意味着我可以用多极操作符调用程序?
{'bread': 1, 'butter': 0, 'cheese': 1, 'water': 0, 'ice cream': 0}
{'bread': 2, 'butter': 1, 'cheese': 2, 'water': 1, 'ice cream': 0}