Python 我能';t遍历列表中的重复值

Python 我能';t遍历列表中的重复值,python,list,dictionary,for-loop,duplicates,Python,List,Dictionary,For Loop,Duplicates,问题在于我列出的年份,如你所见,有些值重复出现,如1932年、1933年、1961年……: years = [1924, 1928, 1932, 1932, 1933, 1933, 1935, 1938, 1953, 1955, 1961, 1961, 1967, 1969, 1971, 1977, 1979, 1980, 1988, 1989, 1992, 1998, 2003, 2004, 2005, 2005, 2005, 2005, 2007, 2007, 2016, 2017, 201

问题在于我列出的年份,如你所见,有些值重复出现,如1932年、1933年、1961年……:

years = [1924, 1928, 1932, 1932, 1933, 1933, 1935, 1938, 1953, 1955, 1961, 1961, 1967, 1969, 1971, 1977, 1979, 1980, 1988, 1989, 1992, 1998, 2003, 2004, 2005, 2005, 2005, 2005, 2007, 2007, 2016, 2017, 2017, 2018]
所以我的职责是创建一个函数,遍历我的所有列表(len 34,所有列表),但我的输出只返回字典中的一个重复年份,所以我的结果输出是26 len,而不是34

def dictionary_maker2(names, months, years, max_sustained_winds, areas_affected, list_update_damage, deaths):
    for n in range(len(years)):
        hurricanes_year[years[n]] = {'Names': names[n], 'Month': months[n], 'Year': years[n],
                                     'Max Sustained Wind': max_sustained_winds[n], 'Areas Affected': areas_affected[n],
                                     'Damage': list_update_damage[n], 'Deaths': deaths[n]}


如果我没有用正确的方式在stackoverflow中提问,请告诉我,这是我在这里的第一个问题。

python字典不能有重复的键,因此,同一年迭代中的最后一项会覆盖现有值。在这种情况下,您可以使用

如果要使用所有存储的值遍历所有年份

for hurricanes in hurricanes_year:
    for hurricane in hurricanes:
        print(hurricane)

如上所述,我不认为您可以将重复的年份存储为与每年相关联的子字典的字典,因为每年只能存储一个飓风数据。为了每年存储多个飓风数据,我认为您应该将
飓风\u year
设置为一个字典,该字典具有与包含该年飓风数据列表(字典)的列表(而不是字典)关联的年份

def dictionary_maker2(names, months, years, max_sustained_winds, areas_affected, list_update_damage, deaths):
    for n in range(len(years)):
        # Check if years[n] not exists in dictionary, if not then initialize empty list
        if years[n] not in hurricanes_year:
            hurricanes_year[years[n]] = []
        # Append hurricane data to the list associate with years[n]
        hurricanes_year[years[n]].append({'Names': names[n], 'Month': months[n], 'Year': years[n],
                                          'Max Sustained Wind': max_sustained_winds[n], 'Areas Affected': areas_affected[n],
                                          'Damage': list_update_damage[n], 'Deaths': deaths[n]})

的确,dict作为哈希表不能为同一个键保存多个值

为了解决这个问题,您可以尝试将每年的列表或记录存储在dict中,并使用
append()
值代替替换

{:[{:,…},…},…}
及供参考:

  • 您可以使用
    zip()
    将单独的值列表合并到一个元组列表中
  • 元组可以以相同的方式与dict键组合,这允许我们在不命名每个字段的情况下构建dict
这可能有助于简化您的表达式。例如:

字段名称=[
“姓名”,
“月”,
“年”,
“最大持续风力”,
“受影响区域”,
“列表\更新\损坏”,
"死亡",,
]
def列至记录(*列):
#将列列表转换为DICT生成器
#像{:}
返回(针对zip(*列)中的值的dict(zip(字段名称、值))
按年度划分的def组(记录):
res={}
对于记录中的rec:
年份=记录[“年份”]
group=res.setdefault(年份,[])
分组追加(rec)
返回res
def打印组(组):
打印('Year\t记录')
对于年份,已排序的记录(groups.items()):
打印(f'{year}\t{records}')
记录=列到列记录(*列)#或(年份,…,smth_其他)
groups=按年份分组(记录){:[{…},{…}]}
打印组(组)

我找到了解决方案。诀窍在于if-else语句。因此,如果我的年份重复,我会将我的值附加到同一个键中。因此,我的最终输出将类似于'1928key':[{1928values}],'1932key':[{1932values,1932values}]

def create_year_dictionary(hurricanes):
hurricanes_by_year= dict()
for i in hurricanes:
    current_year = hurricanes[i]['Year']
    current_cane = hurricanes[i]
    if current_year not in hurricanes_by_year:
        hurricanes_by_year[current_year] = [current_cane]
    else:
        hurricanes_by_year[current_year].append(current_cane)
return hurricanes_by_year

print(create_year_dictionary(hurricanes))

字典不能有重复的键。为了澄清@Selcuk提到的内容,因为字典不能有重复的键,当访问您在上一次迭代中已经访问的年份值并向字典添加值时,它将用新的
{'Names':'',…}
字典覆盖与该年关联的当前值
def create_year_dictionary(hurricanes):
hurricanes_by_year= dict()
for i in hurricanes:
    current_year = hurricanes[i]['Year']
    current_cane = hurricanes[i]
    if current_year not in hurricanes_by_year:
        hurricanes_by_year[current_year] = [current_cane]
    else:
        hurricanes_by_year[current_year].append(current_cane)
return hurricanes_by_year