本文介绍了将字典的值更改为python中的字典的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个真实的字典:一个荷兰语到英语字典,含有以下单词:
def reversTranslation(dictionary):

 >>> dictionary = {'tension':['spanning'],'voltage':['spanning','voltage']} 
>>> dictionary = reverseTranslation(dictionary)
>>>字典
{'spanning':['tension','voltage'],'voltage':['voltage']}

如你所见,荷兰语跨越有两种不同的英文含义。
帮助将不胜感激。

解决方案

你去:



$ d
return dict((v1,[k for k,v in d.iteritems()if v1 in v])
for v1 in set(sum(d.values(),[])))


I want to create a "real" dictionary: a Dutch to English dictionary with the following words:def reversTranslation(dictionary):

>>> dictionary= {'tension': ['spanning'], 'voltage': ['spanning', 'voltage']}
>>> dictionary= reverseTranslation(dictionary)
>>> dictionary
{'spanning': ['tension', 'voltage'], 'voltage': ['voltage']}

As you can see in dutch 'spanning' has two different meanings in English.Help will be appreciated.

解决方案

Here you go:

def reverseTranslation(d):
    return dict((v1,[k for k,v in d.iteritems() if v1 in v])
                for v1 in set(sum(d.values(),[])))

这篇关于将字典的值更改为python中的字典的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:00