【发布时间】:2021-12-29 16:12:21
【问题描述】:
我可以使用一些帮助来解决这个问题。我需要创建一个函数来创建一个带有 4 个参数的字典和一个增加该字典每个条目的键。到目前为止,我有这个:
def create_db(temp, rain, humidity, wind):
weather = {}
n = 0
for i in (temp, rain, humidity, wind):
n = n + 1
weather[n] = (temp, rain, humidity, wind)
return weather
temp = [1, 5, 3]
rain = [0, 30, 100]
humidity = [30, 50, 65]
wind = [3, 5, 7]
weather = create_db(temp, rain, humidity, wind)
print(weather)
这段代码的问题在于它会打印:
{1: (1, 0, 30, 3), 2: (1, 0, 30, 3), 3: (1, 0, 30, 3), 4: (1, 0, 30, 3)}
它只为它们放入列表的第一个值。 我做错了什么?
【问题讨论】:
标签: python function dictionary