将Python字典中的键转换为值,反之亦然。

huangapple go评论74阅读模式
英文:

Python dictonary convert keys to values and vice versa

问题

我需要将键转换为值,值转换为键.. 我正在使用Python 2.7版本...
如果有更好的方法,请告诉我

source_info = \
    {'benz': 'car',
     'audi': 'car',
     'bmw': 'car',
     'toyata': 'car',
     'ferrai': 'car',
     'ducati': 'bike',
     'suzki': 'bike',
     'yamaha': 'bike'
     }

def format_data(source_info=None):
   # {} 转换为集合
   id_unique = {source_info[key_id] for key_id in source_info}
   result_dict = {}
   # 通过算法查找包含键的 seq_def 的结果数据
   for key in id_unique:
    vechile_name = {k: v for k, v in source_info.items() if v in [key]}
    result_dict[key] = list(vechile_name.keys())
   return result_dict

result = format_data(source_info)
print(result)

我正在使用上面的代码完成我的工作.. 但想知道是否有更好的方法?

结果看起来像这样:

{'bike': ['ducati', 'suzki', 'yamaha'], 'car': ['benz', 'audi', 'bmw', 'toyata', 'ferrai']}

非常感谢你的意见...

英文:

I need to convert keys to values and values to key.. i am using python 2.7 version...
if there is any better approach then this let me know

source_info=\
    {'benz':   'car',
     'audi':   'car',
     'bmw':    'car',
     'toyata': 'car',
     'ferrai': 'car',
     'ducati': 'bike',
     'suzki':  'bike',
     'yamaha': 'bike'
     }

def format_data(source_info=None):
   # {} converting to set
   id_unique = {source_info[key_id] for key_id in source_info}
   result_dict = {}
   # Result data from seq_def with searching for key with algos
   for key in id_unique:
    vechile_name = {k: v for k, v in source_info.items() if v in [key]}
    result_dict[key] = list(vechile_name.keys())
   return result_dict

result = format_data(source_info)
print(result)

I'm using the above code to get my job done.. but wanted to know if there is any better approach then this ?

result looks like this:

{'bike': ['ducati', 'suzki', 'yamaha'], 'car': ['benz', 'audi', 'bmw', 'toyata', 'ferrai']}

Highly appreciate your inputs here...

答案1

得分: 4

Readily accomplished with collections.defaultdict.

>>> source_info = {
...     'benz': 'car',
...     'audi': 'car',
...     'bmw': 'car',
...     'toyata': 'car',
...     'ferrai': 'car',
...     'ducati': 'bike',
...     'suzki': 'bike',
...     'yamaha': 'bike'
... }
>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> for k, v in source_info.items():
...     d[v].append(k)
...
>>> d
defaultdict(<type 'list'>, {'car': ['toyata', 'ferrai', 'bmw', 'benz', 'audi'], 'bike': ['suzki', 'yamaha', 'ducati']})
>>> dict(d)
{'car': ['toyata', 'ferrai', 'bmw', 'benz', 'audi'], 'bike': ['suzki', 'yamaha', 'ducati']}
英文:

Readily accomplished with collections.defaultdict.

&gt;&gt;&gt; source_info=\
...     {&#39;benz&#39;:   &#39;car&#39;,
...      &#39;audi&#39;:   &#39;car&#39;,
...      &#39;bmw&#39;:    &#39;car&#39;,
...      &#39;toyata&#39;: &#39;car&#39;,
...      &#39;ferrai&#39;: &#39;car&#39;,
...      &#39;ducati&#39;: &#39;bike&#39;,
...      &#39;suzki&#39;:  &#39;bike&#39;,
...      &#39;yamaha&#39;: &#39;bike&#39;
...      }
&gt;&gt;&gt; from collections import defaultdict
&gt;&gt;&gt; d = defaultdict(list)
&gt;&gt;&gt; for k, v in source_info.items():
...     d[v].append(k)
...
&gt;&gt;&gt; d
defaultdict(&lt;type &#39;list&#39;&gt;, {&#39;car&#39;: [&#39;toyata&#39;, &#39;ferrai&#39;, &#39;bmw&#39;, &#39;benz&#39;, &#39;audi&#39;], &#39;bike&#39;: [&#39;suzki&#39;, &#39;yamaha&#39;, &#39;ducati&#39;]})
&gt;&gt;&gt; dict(d)
{&#39;car&#39;: [&#39;toyata&#39;, &#39;ferrai&#39;, &#39;bmw&#39;, &#39;benz&#39;, &#39;audi&#39;], &#39;bike&#39;: [&#39;suzki&#39;, &#39;yamaha&#39;, &#39;ducati&#39;]}

答案2

得分: 1

To convert keys to values:

source_info = {'benz': 'car', 'audi': 'car', 'bmw': 'car', 'toyata': 'car', 'ferrai': 'car', 'ducati': 'bike', 'suzki': 'bike', 'yamaha': 'bike'}

result_dict = {v: k for k, v in source_info.items()}

print(result_dict)

Output:

{'car': 'ferrai', 'bike': 'yamaha'}

To convert values to keys:

source_info = {'benz': 'car', 'audi': 'car', 'bmw': 'car', 'toyata': 'car', 'ferrai': 'car', 'ducati': 'bike', 'suzki': 'bike', 'yamaha': 'bike'}

result_dict = {k: [i for i in source_info.keys() if source_info[i] == k] for k in set(source_info.values())}

print(result_dict)

Output:

{'car': ['benz', 'audi', 'bmw', 'toyata', 'ferrai'], 'bike': ['ducati', 'suzki', 'yamaha']}

英文:

use dictionary comprehension to convert keys to values and values to keys. I've provided code as below.

To convert keys to values:

source_info = {&#39;benz&#39;: &#39;car&#39;, &#39;audi&#39;: &#39;car&#39;, &#39;bmw&#39;: &#39;car&#39;, &#39;toyata&#39;: &#39;car&#39;, &#39;ferrai&#39;: &#39;car&#39;, &#39;ducati&#39;: &#39;bike&#39;, &#39;suzki&#39;: &#39;bike&#39;, &#39;yamaha&#39;: &#39;bike&#39;}

result_dict = {v: k for k, v in source_info.items()}

print(result_dict)

output like this: {&#39;car&#39;: &#39;ferrai&#39;, &#39;bike&#39;: &#39;yamaha&#39;}

To convert values to keys:

source_info = {&#39;benz&#39;: &#39;car&#39;, &#39;audi&#39;: &#39;car&#39;, &#39;bmw&#39;: &#39;car&#39;, &#39;toyata&#39;: &#39;car&#39;, &#39;ferrai&#39;: &#39;car&#39;, &#39;ducati&#39;: &#39;bike&#39;, &#39;suzki&#39;: &#39;bike&#39;, &#39;yamaha&#39;: &#39;bike&#39;}

result_dict = {k: [i for i in source_info.keys() if source_info[i] == k] for k in set(source_info.values())}

print(result_dict)

output like this: {&#39;car&#39;: [&#39;benz&#39;, &#39;audi&#39;, &#39;bmw&#39;, &#39;toyata&#39;, &#39;ferrai&#39;], &#39;bike&#39;: [&#39;ducati&#39;, &#39;suzki&#39;, &#39;yamaha&#39;]}

let me know if you face any issue.

答案3

得分: 1

You can use dict built-in functionality setdefault.

结果:

{'car': ['benz', 'audi', 'bmw', 'toyata', 'ferrai'], 'bike': ['ducati', 'suzki', 'yamaha']}
英文:

You can use dict built in functionality setdefault

results = {}
for k, v in source_info.items():
    results.setdefault(v, []).append(k)

Output

{&#39;car&#39;: [&#39;benz&#39;, &#39;audi&#39;, &#39;bmw&#39;, &#39;toyata&#39;, &#39;ferrai&#39;], &#39;bike&#39;: [&#39;ducati&#39;, &#39;suzki&#39;, &#39;yamaha&#39;]}

答案4

得分: 0

from itertools import groupby
from operator import itemgetter

{
    k: list(map(itemgetter(0), v))
    for k, v in groupby(sorted(source_info.items(), key=itemgetter(1)), itemgetter(1))
}

输出:

{
    'bike': ['ducati', 'suzki', 'yamaha'],
    'car': ['benz', 'audi', 'bmw', 'toyata', 'ferrai']
}
英文:
from itertools import groupby
from operator import itemgetter

{
    k: list(map(itemgetter(0), v))
    for k, v in groupby(sorted(source_info.items(), key=itemgetter(1)), itemgetter(1))
}

#output
{&#39;bike&#39;: [&#39;ducati&#39;, &#39;suzki&#39;, &#39;yamaha&#39;],
 &#39;car&#39;: [&#39;benz&#39;, &#39;audi&#39;, &#39;bmw&#39;, &#39;toyata&#39;, &#39;ferrai&#39;]}

huangapple
  • 本文由 发表于 2023年5月14日 14:15:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定