配对字典中元素的组合而无需重复 [英] Pair combinations of elements in dictionary without repetition
问题描述
在python中,我有一个这样的字典...
In python, I have a dictionary like this...
pleio = {'firstLine': {'enf1': ['54', 'set'],
'enf2': ['48', 'free'],
'enf3': ['34', 'set'],
'enf4': ['12', 'free']}
'secondLine':{'enf5': ['56','bgb']
'enf6': ['67','kiol']
'enf7': ['11','dewd']
'enf8': ['464','cona']}}
我想在不重复内部字典中元素的情况下进行配对组合,以得到这样的结果。 ..
I would like to make paired combinations with no repetition of the elements in the inner dictionary, to end up with a result like this...
{'enf3': ['34', 'set'], 'enf2': ['48', 'free']}
{'enf3': ['34', 'set'], 'enf1': ['54', 'set']}
{'enf3': ['34', 'set'], 'enf4': ['12', 'free']}
{'enf2': ['48', 'free'], 'enf1': ['54', 'set']}
{'enf2': ['48', 'free'], 'enf4': ['12', 'free']}
{'enf1': ['54', 'set'], 'enf4': ['12', 'free']}
我构建了一个函数,可以让我做...
I built a function which lets me do it...
import itertools
def pairwise():
'''
'''
leti=[]
for snp, enfs in pleio.items():
for x in itertools.combinations(enfs, 2 ):
leti.append(x)
pleopairs=[]
for i in leti:
pipi={}
for c in i:
pipi[c]= enfs[c]
pleopairs.append(pipi)
..但是我想知道是否有更有效的方法,例如itertools的另一个特定功能或任何其他来源。顺便说一下,我在 itertools 文档中找到了一个称为成对的函数。但是我不知道如何适应它,或者我是否会尝试改进。有帮助吗?
..but i was wondering if there's a more efficient way, like another specific function from itertools, or any other source. By the way, I found a function called "pairwise" in the itertools documentation. But I don't know how to adapt it, if would be possible in my case, or improve my attempt. Any help?
推荐答案
您的组合
方法是正确的,您只需要再次将每个组合的结果转换为字典:
Your combinations
approach was correct, you just need to turn the results of each combination into a dict again:
import itertools
def pairwise(input):
for values in input.itervalues():
for pair in itertools.combinations(values.iteritems(), 2):
yield dict(pair)
此版本是一个生成器,可以高效地生成对,没有什么比绝对必要的存储在内存中了。如果需要列表,只需在生成器上调用 list()
:
This version is a generator, yielding pairs efficiently, nothing is held in memory any longer than absolutely necessary. If you need a list, just call list()
on the generator:
list(pairwise(pleio))
输出:
>>> from pprint import pprint
>>> pprint(list(pairwise(pleio)))
[{'enf2': ['48', 'free'], 'enf3': ['34', 'set']},
{'enf1': ['54', 'set'], 'enf3': ['34', 'set']},
{'enf3': ['34', 'set'], 'enf4': ['12', 'free']},
{'enf1': ['54', 'set'], 'enf2': ['48', 'free']},
{'enf2': ['48', 'free'], 'enf4': ['12', 'free']},
{'enf1': ['54', 'set'], 'enf4': ['12', 'free']}]
您甚至可以合并整个将东西放到单层生成器中:
You can even combine the whole thing into a one-liner generator:
from itertools import combinations
for paired in (dict(p) for v in pleio.itervalues() for p in combinations(v.iteritems(), 2)):
print paired
哪些输出:
>>> for paired in (dict(p) for v in pleio.itervalues() for p in combinations(v.iteritems(), 2)):
... print paired
...
{'enf3': ['34', 'set'], 'enf2': ['48', 'free']}
{'enf3': ['34', 'set'], 'enf1': ['54', 'set']}
{'enf3': ['34', 'set'], 'enf4': ['12', 'free']}
{'enf2': ['48', 'free'], 'enf1': ['54', 'set']}
{'enf2': ['48', 'free'], 'enf4': ['12', 'free']}
{'enf1': ['54', 'set'], 'enf4': ['12', 'free']}
如果您使用的是Python 3,请替换 .itervalues()
并 .iteritems()
由 .values()
和 .items()
。
If you are on Python 3, replace .itervalues()
and .iteritems()
by .values()
and .items()
respectively.
这篇关于配对字典中元素的组合而无需重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!