Say I have a dictionary like so:
说我有这样的字典:
my_dict = {2:3, 5:6, 8:9}
Is there a way that I can switch the keys and values to get:
有没有办法可以切换键和值来获得:
{3:2, 6:5, 9:8}
7 个解决方案
#1
53
my_dict2 = dict((y,x) for x,y in my_dict.iteritems())
If you are using python 2.7 or 3.x you can use a dictionary comprehension instead:
如果您使用的是python 2.7或3.x,则可以使用字典理解:
my_dict2 = {y:x for x,y in my_dict.iteritems()}
Edit
编辑
As noted in the comments by JBernardo, for python 3.x you need to use items
instead of iteritems
正如JBernardo的评论中所指出的,对于python 3.x,您需要使用项而不是iteritems
#2
6
Use this code (trivially modified) from the accepted answer at Python reverse / invert a mapping:
使用此代码(通过修改)从Python中接受的答案反向/反转映射:
dict((v,k) for k, v in my_dict.iteritems())
Note that this assumes that the values in the original dictionary are unique. Otherwise you'd end up with duplicate keys in the resulting dictionary, and that is not allowed.
请注意,这假定原始字典中的值是唯一的。否则,您最终会在结果字典中出现重复的密钥,这是不允许的。
And, as @wim points out, it also assumes the values are hashable. See the Python glossary if you're not sure what is and isn't hashable.
并且,正如@wim指出的那样,它还假设值是可以清除的。如果您不确定什么是可用的,那么请参阅Python词汇表。
#3
4
Try this:
尝试这个:
my_dict = {2:3, 5:6, 8:9}
new_dict = {}
for k, v in my_dict.items():
new_dict[v] = k
#4
4
maybe:
也许:
flipped_dict = dict(zip(my_dict.values(), my_dict.keys()))
flipped_dict = dict(zip(my_dict.values(),my_dict.keys()))
#5
2
Sometimes, the condition that the values are all unique will not hold, in which case, the answers above will destroy any duplicate values.
有时,值都是唯一的条件将不成立,在这种情况下,上述答案将销毁任何重复值。
The following rolls the values that might be duplicates up into a list:
以下内容将可能重复的值滚动到列表中:
from itertools import count
dict([(a,[list(d.keys())[i] for i,j in zip(count(), d.values())if j==a in set(d.values())])
I'm sure there's a better (non-list-comp) method, but I had a problem with the earlier answers, so thought I'd provide my solution in case others have a similar use-case.
我确信有一个更好的(非list-comp)方法,但我对前面的答案有疑问,所以我想我会提供我的解决方案以防其他人有类似的用例。
P.S. Don't expect the dict to remain neatly arranged after any changes to the original! This method is a one-time use only on a static dict - you have been warned!
附:在对原版进行任何更改后,不要指望dict保持整齐排列!此方法仅在静态字典上一次性使用 - 您已被警告过!
#6
1
my_dict = { my_dict[k]:k for k in my_dict}
#7
1
First of all it is not guaranteed that this is possible, since the values of a dictionary can be unhashable.
首先,不能保证这是可能的,因为字典的值可能是不可用的。
In case these are not, we can use a functional approach with:
如果不是,我们可以使用功能方法:
reversed_dict = dict(map(reversed, original_dict.items()))
#1
53
my_dict2 = dict((y,x) for x,y in my_dict.iteritems())
If you are using python 2.7 or 3.x you can use a dictionary comprehension instead:
如果您使用的是python 2.7或3.x,则可以使用字典理解:
my_dict2 = {y:x for x,y in my_dict.iteritems()}
Edit
编辑
As noted in the comments by JBernardo, for python 3.x you need to use items
instead of iteritems
正如JBernardo的评论中所指出的,对于python 3.x,您需要使用项而不是iteritems
#2
6
Use this code (trivially modified) from the accepted answer at Python reverse / invert a mapping:
使用此代码(通过修改)从Python中接受的答案反向/反转映射:
dict((v,k) for k, v in my_dict.iteritems())
Note that this assumes that the values in the original dictionary are unique. Otherwise you'd end up with duplicate keys in the resulting dictionary, and that is not allowed.
请注意,这假定原始字典中的值是唯一的。否则,您最终会在结果字典中出现重复的密钥,这是不允许的。
And, as @wim points out, it also assumes the values are hashable. See the Python glossary if you're not sure what is and isn't hashable.
并且,正如@wim指出的那样,它还假设值是可以清除的。如果您不确定什么是可用的,那么请参阅Python词汇表。
#3
4
Try this:
尝试这个:
my_dict = {2:3, 5:6, 8:9}
new_dict = {}
for k, v in my_dict.items():
new_dict[v] = k
#4
4
maybe:
也许:
flipped_dict = dict(zip(my_dict.values(), my_dict.keys()))
flipped_dict = dict(zip(my_dict.values(),my_dict.keys()))
#5
2
Sometimes, the condition that the values are all unique will not hold, in which case, the answers above will destroy any duplicate values.
有时,值都是唯一的条件将不成立,在这种情况下,上述答案将销毁任何重复值。
The following rolls the values that might be duplicates up into a list:
以下内容将可能重复的值滚动到列表中:
from itertools import count
dict([(a,[list(d.keys())[i] for i,j in zip(count(), d.values())if j==a in set(d.values())])
I'm sure there's a better (non-list-comp) method, but I had a problem with the earlier answers, so thought I'd provide my solution in case others have a similar use-case.
我确信有一个更好的(非list-comp)方法,但我对前面的答案有疑问,所以我想我会提供我的解决方案以防其他人有类似的用例。
P.S. Don't expect the dict to remain neatly arranged after any changes to the original! This method is a one-time use only on a static dict - you have been warned!
附:在对原版进行任何更改后,不要指望dict保持整齐排列!此方法仅在静态字典上一次性使用 - 您已被警告过!
#6
1
my_dict = { my_dict[k]:k for k in my_dict}
#7
1
First of all it is not guaranteed that this is possible, since the values of a dictionary can be unhashable.
首先,不能保证这是可能的,因为字典的值可能是不可用的。
In case these are not, we can use a functional approach with:
如果不是,我们可以使用功能方法:
reversed_dict = dict(map(reversed, original_dict.items()))