Python字典理解,用于字典列表

Python字典理解,用于字典列表,python,dictionary,Python,Dictionary,我想从下面的列表中创建一个字典 [{'fips': '01001', 'state': 'AL', 'name': 'Autauga County'}, {'fips': '20005', 'state': 'KS', 'name': 'Atchison County'}, {'fips': '47145', 'state': 'TN', 'name': 'Roane County'}] 结果应以名称为键,以“美国”为值 例如: 我可以通过几个for循环来实现这一点,但我想学习如何使用字典理解

我想从下面的列表中创建一个字典

[{'fips': '01001', 'state': 'AL', 'name': 'Autauga County'}, {'fips': '20005', 'state': 'KS', 'name': 'Atchison County'}, {'fips': '47145', 'state': 'TN', 'name': 'Roane County'}]
结果应以名称为键,以“美国”为值

例如:

我可以通过几个for循环来实现这一点,但我想学习如何使用字典理解来实现这一点

in_list = [{'fips': '01001', 'state': 'AL', 'name': 'Autauga County'}, 
           {'fips': '20005', 'state': 'KS', 'name': 'Atchison County'},
           {'fips': '47145', 'state': 'TN', 'name': 'Roane County'}]

out_dict = {x['name']: 'United States' for x in in_list if 'name' in x}
学习的一些注意事项:

  • 理解仅适用于Python2.7以后的版本
  • 字典理解与列表理解非常相似,只是有大括号
    {}
    (和键)
  • 如果您不知道,还可以在理解中的for循环之后添加更复杂的控制流,例如
    [x for x In some_list if(cond)]
为了完整起见,如果你不能使用理解,那么试试这个

out_dict = {}

for dict_item in in_list:
    if not isinstance(dict_item, dict):
        continue

    if 'name' in dict_item:
        in_name = dict_item['name']
        out_dict[in_name] = 'United States'
如注释中所述,对于Python2.6,您可以将
{k:v for k,v in iterator}
替换为:

dict((k,v) for k,v in iterator)
你可以阅读更多关于这方面的内容


快乐编码

回路/生成器版本为:

location_list = [{'fips': '01001', 'state': 'AL', 'name': 'Autauga County'},
        {'fips': '20005', 'state': 'KS', 'name': 'Atchison County'},
        {'fips': '47145', 'state': 'TN', 'name': 'Roane County'}]
location_dict = {location['name']:'United States' for location in location_list}
输出:

{'Autauga County': 'United States', 'Roane County': 'United States',
 'Atchison County': 'United States'}

如果您在Stackoverflow上搜索字典理解,使用
{}
生成器表达式的解决方案将开始显示:

这里有一个适用于python2.7.x和Python3.x的小解决方案:

data = [
    {'fips': '01001', 'state': 'AL', 'name': 'Autauga County'},
    {'fips': '20005', 'state': 'KS', 'name': 'Atchison County'},
    {'fips': '47145', 'state': 'TN', 'name': 'Roane County'},
    {'fips': 'xxx', 'state': 'yyy'}
]

output = {item['name']: 'United States' for item in data if 'name' in item}
print(output)

那应该对你有好处

states_dict = [{'fips': '01001', 'state': 'AL', 'name': 'Autauga County'}, {'fips': '20005', 'state': 'KS', 'name': 'Atchison County'}, {'fips': '47145', 'state': 'TN', 'name': 'Roane County'}]

{states_dict[i]['name']:'United States' for i, elem in enumerate(states_dict)}

您可能需要指定您正在使用的Python版本。我的坏习惯是Python 3。不过我必须说,这是我得到的最快的反对票——是因为缺少Python 2支持还是其他原因?@tehjoker是的,好吧,字典理解只适用于Python 2.7+,所以这是一个没有意义的点……我完全不明白为什么会有反对票。写一个Python2.7或3的答案不要感到难过,在问题中没有版本号Python3是一个很好的假设。我也很困惑。我认为可以安全地假设,当问题问及词典理解时,我们谈论的是一个支持词典理解的Python版本……哈哈,谢谢大家——我对rep不是特别在意,我认为回答问题是学习编码的一个很好的方式——而否决票只是确保您生成了最好的代码!另一方面,有人应该给出一个同时支持Python 2.x和3.x的答案,我很期待它(有一段时间没有做Python 2了)
dict
list
都是不好用的名字,因为它们隐藏了内置程序。为了更好地理解,有什么问题吗?。我相信它工作得很好,我已经测试过了it@BlackHawk这是一种令人困惑和不雅的方式来完成一些可以做得简单得多的事情(见顶级答案)。我还在学习,只是想帮忙。
states_dict = [{'fips': '01001', 'state': 'AL', 'name': 'Autauga County'}, {'fips': '20005', 'state': 'KS', 'name': 'Atchison County'}, {'fips': '47145', 'state': 'TN', 'name': 'Roane County'}]

{states_dict[i]['name']:'United States' for i, elem in enumerate(states_dict)}