更新字典中的键值
Updating key values in dictionaries
我正在尝试为以下内容编写代码:
这个想法是有一个 storage/inventory 字典,然后通过某些家务任务减少键值。例如。清洁、烹饪等
这将是存储字典:
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}
现在这是我编写的代码,用于尝试减少一个键的值(想法是“清洁”操作将键“清洁喷雾”减少一个清洁单位 = 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)
但是它returns这个,它和以前的字典是一样的,没有更新值
{'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}
有人可以帮忙吗?
谢谢!!
附上图片以查看缩进等
所以我认为值没有改变的原因是因为它是在 for 循环中完成的。
例如
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
这个returns:
list_values [1, 2, 3, 4, 5]
new_variable [2, 3, 4, 5, 6]
所以要解决这个问题,您可以使用 'new_variable'
那么,既然概念已经清楚了(我希望),在你的情况下它会是这样的
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
我们return就是cleaned
'new_variable'
因此,如果需要,可以按如下方式将其分配给原始字典变量:
cupboard = cleaning()
编辑:
另外,正如@d-k-bo 评论的那样,如果您打算只执行一次操作...... if 语句也可以完成这项工作
if cupboard["cleaningspray"] > 0.5: # Again assuming you intended '>' and not '<'
否则,您应该将 return 语句保留在 while 循环之外
我猜您想根据房间大小(或其他因素)减少“清洁喷雾”的用量。我会这样做:
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)
输出:
{'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}
我正在尝试为以下内容编写代码: 这个想法是有一个 storage/inventory 字典,然后通过某些家务任务减少键值。例如。清洁、烹饪等
这将是存储字典:
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}
现在这是我编写的代码,用于尝试减少一个键的值(想法是“清洁”操作将键“清洁喷雾”减少一个清洁单位 = 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)
但是它returns这个,它和以前的字典是一样的,没有更新值
{'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}
有人可以帮忙吗?
谢谢!!
附上图片以查看缩进等
所以我认为值没有改变的原因是因为它是在 for 循环中完成的。
例如
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
这个returns:
list_values [1, 2, 3, 4, 5]
new_variable [2, 3, 4, 5, 6]
所以要解决这个问题,您可以使用 'new_variable'
那么,既然概念已经清楚了(我希望),在你的情况下它会是这样的
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
我们return就是cleaned
'new_variable'
因此,如果需要,可以按如下方式将其分配给原始字典变量:
cupboard = cleaning()
编辑: 另外,正如@d-k-bo 评论的那样,如果您打算只执行一次操作...... if 语句也可以完成这项工作
if cupboard["cleaningspray"] > 0.5: # Again assuming you intended '>' and not '<'
否则,您应该将 return 语句保留在 while 循环之外
我猜您想根据房间大小(或其他因素)减少“清洁喷雾”的用量。我会这样做:
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)
输出:
{'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}