英文:
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
.
>>> 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']}
答案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 = {'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 like this: {'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 like this: {'car': ['benz', 'audi', 'bmw', 'toyata', 'ferrai'], 'bike': ['ducati', 'suzki', 'yamaha']}
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
{'car': ['benz', 'audi', 'bmw', 'toyata', 'ferrai'], 'bike': ['ducati', 'suzki', 'yamaha']}
答案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
{'bike': ['ducati', 'suzki', 'yamaha'],
'car': ['benz', 'audi', 'bmw', 'toyata', 'ferrai']}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论