[英]Updating key values in dictionaries
I am trying to write code for the following: The idea is to have a storage/inventory dictionary and then have the key values be reduced by certain household tasks.我正在尝试为以下内容编写代码:想法是拥有一个存储/库存字典,然后通过某些家庭任务减少键值。 Eg cleaning, cooking etc.
例如清洁、烹饪等。
This would be the storage dictionary:这将是存储字典:
cupboard= {"cookies":30,
"coffee":3,
"washingpowder": 5,
"cleaningspray": 5,
'Pasta': 0.5,
'Tomato': 4,
'Beef': 2,
'Potato': 2,
'Flour': 0.2,
'Milk': 1,
"Burger buns": 6}
now this is the code that I wrote to try and reduce one single key's value (idea is that the action "cleaning" reduces the key "cleaning spray" by one cleaning unit = 0.5现在这是我编写的代码,试图减少一个键的值(想法是“清洁”动作将“清洁喷雾”键减少一个清洁单位 = 0.5
cleaning_amount = 0.5
def cleaning(room):
while cupboard["cleaningspray"] <0.5:
cleaned = {key: cupboard.get(key) - cleaning_amount for key in cupboard}
return cupboard
livingroom = 1*cleaning_amount
cleaning(livingroom)
print(cupboard)
but it returns this, which is the same dictionary as before, with no updated values但它返回 this,它与以前的字典相同,没有更新值
{'cookies': 30, 'coffee': 3, 'washingpowder': 5, 'cleaningspray': 5, 'Pasta': 0.5, 'Tomato': 4, 'Beef': 2, 'Potato': 2, 'Flour': 0.2, 'Milk': 1, 'Burger buns': 6}
Can anybody help?有人可以帮忙吗?
Thank you!!谢谢!!
picture attached to see indents etc.附上图片以查看缩进等。
So I believe the reason that the values don't change is because it is being done in a for loop.所以我相信值不会改变的原因是因为它是在 for 循环中完成的。
eg例如
list_values = [1, 2, 3, 4, 5]
new_variable = [num + 1 for num in list_values]
print("list_values", list_values) # The original list_values variable doesn't change
print("new_variable", new_variable) # This new variable holds the required value
This returns:这将返回:
list_values [1, 2, 3, 4, 5]
new_variable [2, 3, 4, 5, 6]
So to fix the problem, you can use the 'new_variable'所以要解决这个问题,你可以使用'new_variable'
So, now that the concept is clear (I hope), in your case it would be something like this所以,现在这个概念很清楚(我希望),在你的情况下,它会是这样的
def cleaning():
while cupboard["cleaningspray"] > 0.5: # Also here, i beleive you intend to have `>`
#and not `<` in the original code
cleaned = {key: cupboard.get(key) - cleaning_amount for key in cupboard}
return cleaned
We return the 'new_variable' that is cleaned
so it can be assigned to the original dictionary variable as follows if required: cupboard = cleaning()
我们返回已
cleaned
的“new_variable”,因此如果需要,可以将其分配给原始字典变量,如下所示: cupboard = cleaning()
EDIT: Also, as @dk-bo commented, if you intend to only have the operation carry out once... an if statement would also do the job编辑:另外,正如@dk-bo 评论的那样,如果您打算只执行一次操作...... if 语句也可以完成这项工作
if cupboard["cleaningspray"] > 0.5: # Again assuming you intended '>' and not '<'
Otherwise, you should keep the return statement outside the while loop否则,您应该将 return 语句保留在 while 循环之外
I guess you want to decrease the "cleaningspray" amount depending on the room size (or other factors).我猜你想根据房间大小(或其他因素)减少“清洁喷雾”的数量。 I would do it like this:
我会这样做:
cleaning_amount = 0.5
def cleaning(cleaning_factor):
if cupboard["cleaningspray"] > 0.5:
# reduce the amount of cleaning spray depending on the cleaning_factor and the global cleaning_amount
cupboard["cleaningspray"] -= cleaning_factor * cleaning_amount
livingroom_cleaning_factor = 1
cleaning(livingroom_cleaning_factor)
print(cupboard)
Output: Output:
{'cookies': 30, 'coffee': 3, 'washingpowder': 5, 'cleaningspray': 4.5, 'Pasta': 0.5, 'Tomato': 4, 'Beef': 2, 'Potato': 2, 'Flour': 0.2, 'Milk': 1, 'Burger buns': 6}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.