操作說明:
資料的組織方式使得每個索引處的資料(從 0 到 33)對應于相同的颶風。
例如,names[0] 產生“Cuba I”颶風,該颶風發生在幾個月 [0](十月)年 [0](1924 年)。
撰寫一個函式,構造一個由串列組成的字典,其中字典的鍵是颶風的名稱,值是字典本身,其中包含每個資料的鍵(名稱、月份、年份、最大持續風, Areas Affected, Damage, Death) 關于颶風。
因此,鍵“Cuba I”將具有以下值:{'Name': 'Cuba I', 'Month': 'October', 'Year': 1924, 'Max Sustained Wind': 165, 'Areas Affected': [ “中美洲”、“墨西哥”、“古巴”、“佛羅里達”、“巴哈馬”]、“損害”:“未記錄的損害”、“死亡”:90}。
這些是清單:
names = ['Cuba I', 'San Felipe II Okeechobee', 'Cuba II']
months = ['October', 'September', 'September']
years = [1924, 1928, 1932]
max_sustained_winds = [165, 160, 160]
areas_affected = [['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'], ['The Bahamas', 'Northeastern United States']]
damages = ['Damages not recorded', '100M', 'Damages not recorded', '40M']
deaths = [90,4000,16]
我嘗試過的代碼:
hurricane_records = list(zip(names, months, years, max_sustained_winds, areas_affected, update_damages(), deaths))
dict1 = {}
strongest_hurricane_records = {}
for i in range(len(names)):
dict1["Name"] = hurricane_records[i][0]
dict1["Month"] = hurricane_records[i][1]
dict1["Year"] = hurricane_records[i][2]
dict1["Max Sustained Wind"] = hurricane_records[i][3]
dict1["Areas Affected"] = hurricane_records[i][4]
dict1["Damage"] = hurricane_records[i][5]
dict1["Deaths"] = hurricane_records[i][6]
strongest_hurricane_records[names[i]] = dict1
print(strongest_hurricane_records["Cuba I"])
我的問題是,當我嘗試訪問字典“Cuba I”時,不是列印“Cuba I”字典的值,而是列印最后一個字典,即:
{'Cuba II', 'September', 1928, 160, ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'], '100M', 4000}
uj5u.com熱心網友回復:
您可以列出所有值,然后使用 for 回圈,然后使用 dict(value) 將每個值轉換為字典,然后使用.update() 將這些結果附加到另一個字典中 Features = [all features] Dict = {} for f in Features: Dict.update(dict(f)) else: print(Dict)
uj5u.com熱心網友回復:
問題是您在回圈dict1 = {} 之外for,因此只會創建一個字典,并且回圈的每次迭代都會修改同一個字典。只需dict1 = {}進入回圈,為每次颶風重新初始化它:
strongest_hurricane_records = {}
for i in range(len(names)):
dict1 = {} # Now dict1 is unique for each loop iteration
dict1["Name"] = ...
strongest_hurricane_records[names[i]] = dict1
uj5u.com熱心網友回復:
update_damages()是不可復制的,所以我自己制作了與您的類似的串列。請記住在發布最小作業示例之前運行您的代碼并修復錯誤。
如果我理解正確,您想創建一個嵌套字典,即包含其他字典的字典。我給出一個簡化版。您可以在其他非常相關的問題中找到更高級的內容,例如如何將三個串列壓縮到嵌套的 dict中。
# Outer dictionary:
genders = ['male', 'female']
# Inner dictionary:
keys = ['name', 'surname', 'age']
names = ['Bob', 'Natalie']
surnames = ['Blacksmith', 'Smith']
ages = [10, 20]
inner_dictionaries = [dict(zip(keys, [names[i],
surnames[i],
ages[i]]))
for i, elem in enumerate(names)]
# [{'name': 'Bob', 'surname': 'Blacksmith', 'age': 10},
# {'name': 'Natalie', 'surname': 'Smith', 'age': 20}]
outer_dictionaries = dict((keys, values)
for keys, values in
zip(genders, inner_dictionaries))
# {'male': {'name': 'Bob', 'surname': 'Blacksmith', 'age': 10},
# 'female': {'name': 'Natalie', 'surname': 'Smith', 'age': 20}}
順便說一句range(len()),for如果您想像本地人一樣回圈(也來自R. Hettinger),請避免回圈。繼續前進,inner_dictionaries來自這個 for 回圈:
for index, element in enumerate(names):
print(dict(zip(keys, [names[index], surnames[index], ages[index]])))
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/429457.html
上一篇:ASP.NETMVC/JavaScript將帶有JS和鏈接元素的表單元素添加到模型
下一篇:列印流字串的一部分?
