将字典列表变成单个字典

发布于 2025-01-11 02:35:32 字数 839 浏览 0 评论 0原文

我一直在尝试将字典列表转换为单个字典,其中每个键是 ,,每个值是

简而言之,我有一个看起来像这样的列表。

[{'city': 'Normal', 
  'county': 'Madison County', 
  'state': 'Alabama'},
 {'city': 'Birmingham', 
  'county': 'Jefferson County', 
  'state': 'Alabama'},
 {'city': 'Montgomery', 
  'county': 'Montgomery County', 
  'state': 'Alabama'}]

我想将其更改为类似的内容,

{'Normal, Alabama': 'Madison County',
 'Birmingham, Alabama': 'Jefferson County',
 'Montgomery, Alabama': 'Montgomery County'}

我尝试了字典理解,但似乎无法同时添加两个不同的键。

所以我知道下面的代码将为我提供一本包含城市和县的键值对的字典

new_dict = {x['city']: x['county']  for x in counties}

但是,如何添加两个不同的键?像 new_dict = {x['city','state']: x['county'] for x in counties} 这样的东西是行不通的。

I've been trying to convert a list of dictionaries into a single dictionary in such a way where each key is <city>, <state> and each value is <county>.

In short I have a list that looks like this.

[{'city': 'Normal', 
  'county': 'Madison County', 
  'state': 'Alabama'},
 {'city': 'Birmingham', 
  'county': 'Jefferson County', 
  'state': 'Alabama'},
 {'city': 'Montgomery', 
  'county': 'Montgomery County', 
  'state': 'Alabama'}]

And I would like to change it into something like this

{'Normal, Alabama': 'Madison County',
 'Birmingham, Alabama': 'Jefferson County',
 'Montgomery, Alabama': 'Montgomery County'}

I tried dictionary comprehensions but can't seem to add two different keys at once.

So I know the code below will give me a dictionary with cities and counties for key value pairs

new_dict = {x['city']: x['county']  for x in counties}

However, how do I add two different keys? Something like new_dict = {x['city','state']: x['county'] for x in counties} won't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

调妓 2025-01-18 02:35:32

您可以使用字典理解:

lst = [{'city': 'Normal',
  'county': 'Madison County',
  'state': 'Alabama'},
 {'city': 'Birmingham',
  'county': 'Jefferson County',
  'state': 'Alabama'},
 {'city': 'Montgomery',
  'county': 'Montgomery County',
  'state': 'Alabama'}]

output = {f"{dct['city']}, {dct['state']}": dct['county'] for dct in lst}
print(output)
# {'Normal, Alabama': 'Madison County', 'Birmingham, Alabama': 'Jefferson County', 'Montgomery, Alabama': 'Montgomery County'}

在这里,您使用 f 字符串组合两个键。

You can use dict comprehension:

lst = [{'city': 'Normal',
  'county': 'Madison County',
  'state': 'Alabama'},
 {'city': 'Birmingham',
  'county': 'Jefferson County',
  'state': 'Alabama'},
 {'city': 'Montgomery',
  'county': 'Montgomery County',
  'state': 'Alabama'}]

output = {f"{dct['city']}, {dct['state']}": dct['county'] for dct in lst}
print(output)
# {'Normal, Alabama': 'Madison County', 'Birmingham, Alabama': 'Jefferson County', 'Montgomery, Alabama': 'Montgomery County'}

Here, you combine two keys by using f-string.

转身泪倾城 2025-01-18 02:35:32

您可以使用字符串连接来形成键:

data = [{'city': 'Normal', 
  'county': 'Madison County', 
  'state': 'Alabama'},
 {'city': 'Birmingham', 
  'county': 'Jefferson County', 
  'state': 'Alabama'},
 {'city': 'Montgomery', 
  'county': 'Montgomery County', 
  'state': 'Alabama'}]
  
new_dict = {x['city'] + ", " + x['state']: x['county']  for x in data}

print(new_dict)

这会输出:

{'Normal, Alabama': 'Madison County', 'Birmingham, Alabama': 'Jefferson County', 'Montgomery, Alabama': 'Montgomery County'}

You can use string concatenation to form the key:

data = [{'city': 'Normal', 
  'county': 'Madison County', 
  'state': 'Alabama'},
 {'city': 'Birmingham', 
  'county': 'Jefferson County', 
  'state': 'Alabama'},
 {'city': 'Montgomery', 
  'county': 'Montgomery County', 
  'state': 'Alabama'}]
  
new_dict = {x['city'] + ", " + x['state']: x['county']  for x in data}

print(new_dict)

This outputs:

{'Normal, Alabama': 'Madison County', 'Birmingham, Alabama': 'Jefferson County', 'Montgomery, Alabama': 'Montgomery County'}
指尖微凉心微凉 2025-01-18 02:35:32

为什么不只是一个简单的 for 循环呢?

l = [
    {"city": "Normal", "county": "Madison County", "state": "Alabama"},
    {"city": "Birmingham", "county": "Jefferson County", "state": "Alabama"},
    {"city": "Montgomery", "county": "Montgomery County", "state": "Alabama"},
]

result = dict()
for x in l:
    city = x.get("city")
    state = x.get("state")

    key = f"{city}, {state}"
    value = x.get("county")

    result[key] = value

print(result)

Why not just a simple for loop?

l = [
    {"city": "Normal", "county": "Madison County", "state": "Alabama"},
    {"city": "Birmingham", "county": "Jefferson County", "state": "Alabama"},
    {"city": "Montgomery", "county": "Montgomery County", "state": "Alabama"},
]

result = dict()
for x in l:
    city = x.get("city")
    state = x.get("state")

    key = f"{city}, {state}"
    value = x.get("county")

    result[key] = value

print(result)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文