簡體   English   中英

創建一個包含許多 dicts 的列表

[英]Create a list of many dicts

我想獲得 3 個關於國家/地區的信息列表,然后一個一個地添加到列表中的一個新字典中,我希望它通過組織和清理,所以人口和排名。 所以第一個國家應該是人口最多的國家,這個例子的排名應該是1。

我有 3 個國家/地區的 3 個列表:

countries = ['Argentina', 'Brazil', 'France']
population = ['100', '900', '1400']
ranks = ['1', '2', '3']

result_list_of_dicts = [
    {
        'Country': 'COUNTRY',
        'Rank': 'RANK',
        'Population': 'POPULATION'
    }
    {
        'Country': 'COUNTRY',
        'Rank': 'RANK',
        'Population': 'POPULATION'
    }
    # etc
]

在列表理解中使用zip 這給出了一個元組序列,然后您可以對其進行索引或解包以在字典中創建值。 以下示例使用解包:

countries = ['Argentina', 'Brazil', 'France']
population = ['100', '900', '1400']
ranks = ['1', '2', '3']

result_list_of_dicts = [
    {'Country': country,
     'Rank': rank,
     'Population': pop}
    for country, rank, pop in zip(countries, ranks, population)]

如果您更喜歡使用索引而不是解包,那么它看起來像:

result_list_of_dicts = [
    {'Country': t[0],
     'Rank': t[1],
     'Population': t[2]}
    for t in zip(countries, ranks, population)]

其中t包含一個 3 元素元組。

列表理解只是一種方便,在上述兩種情況下,您都可以使用顯式循環,在該循環中您 append 將單個字典添加到列表中,例如:

result_list_of_dicts = []
for country, rank, pop in zip(countries, ranks, population):
    result_list_of_dicts.append({'Country': country,
                                 'Rank': rank,
                                 'Population': pop})


更新:

根據 TelegramCEO2 的后續評論,要求是按照人口增加的順序對字典進行排序,並且排名應該與這種排序順序相關(在這種情況下,輸入排名列表的目的不明確)。 省略'Rank' ,然后添加適當的后處理,其中對列表進行排序並將'Rank'項添加到字典中,可以獲得以下結果:

countries = ['Argentina', 'Brazil', 'France']
population = ['100', '900', '1400']

result_list_of_dicts = [
    {'Country': country,
     'Population': pop}
    for country, pop in zip(countries, population)]

result_list_of_dicts.sort(key=lambda d:-int(d['Population']))

for (i, d) in enumerate(result_list_of_dicts):
    d['Rank'] = str(1 + i)
        
print(result_list_of_dicts)

這使:

[{'Country': 'France', 'Population': '1400', 'Rank': '1'}, {'Country': 'Brazil', 'Population': '900', 'Rank': '2'}, {'Country': 'Argentina', 'Population': '100', 'Rank': '3'}]

以下是如何使用zip()

countries = ['Argentina', 'Brazil', 'France']
population = ['100', '900', '1400']
ranks = ['1', '2', '3']

result_list_of_dicts = [{'country': c,
                         'rank': r,
                         'populations': p}
                        for c, p, r in
                        zip(countries, population, ranks)]

print(result_list_of_dicts)

Output:

[{'country': 'Argentina', 'rank': '1', 'populations': '100'},
 {'country': 'Brazil', 'rank': '2', 'populations': '900'},
 {'country': 'France', 'rank': '3', 'populations': '1400'}]

更新:

事實證明,應該對人口進行排序,使人口最多的進入列表中的第一個國家,最小的人口進入列表中的最后一個國家,並且排名按每個國家的人口數量排列:

countries = ['Argentina', 'Brazil', 'France']
population = ['100', '900', '1400']
ranks = ['1', '2', '3']


population = sorted(population,reverse=True,key=lambda x:int(x.replace(',','')))
ranks = sorted(ranks,key=lambda x:int(x))

result_list_of_dicts = [{'country': c,
                         'rank': r,
                         'populations': p}
                        for c, p, r in
                        zip(countries,
                            population,
                            ranks)]

print(result_list_of_dicts)

Output:

[{'country': 'Argentina', 'rank': '1', 'populations': '1400'},
 {'country': 'Brazil', 'rank': '2', 'populations': '900'},
 {'country': 'France', 'rank': '3', 'populations': '100'}]


既然我們知道排名是一致的,那么ranks列表可以完全省略:

countries = ['Argentina', 'Brazil', 'France']
population = ['100', '900', '1400']

population = sorted(population,reverse=True,key=lambda x:int(x.replace(',','')))

result_list_of_dicts = [{'country': c,
                         'rank': r,
                         'populations': p}
                        for r, (c, p) in
                        enumerate(zip(countries,
                                      population), 1)]

print(result_list_of_dicts)

Output:

[{'country': 'Argentina', 'rank': 1, 'populations': '1400'},
 {'country': 'Brazil', 'rank': 2, 'populations': '900'},
 {'country': 'France', 'rank': 3, 'populations': '100'}]

這段代碼應該可以完成這項工作:

result_list_of_dicts = []
for i in range(len(countries)):
    result_list_of_dicts.append({'country': countries[i], 'rank': ranks[i],  'population': population[i]})

結合理解使用zip

countries = ['Argentina', 'Brazil', 'France']
population = ['100', '900', '1400']
ranks = ['1', '2', '3']

result_list_of_dicts = [{"Country": country, "Rank": rank, "Population": pop}
                        for country, rank, pop in zip(countries, ranks, population)]
print(result_list_of_dicts)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM