简体   繁体   English

配对字典中元素的组合而无需重复

[英]Pair combinations of elements in dictionary without repetition

In python, I have a dictionary like this... 在python中,我有一个像这样的字典...

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)

..but i was wondering if there's a more efficient way, like another specific function from itertools, or any other source. ..但我想知道是否有更有效的方法,例如itertools中的另一个特定功能,或任何其他来源。 By the way, I found a function called "pairwise" in the itertools documentation. 顺便说一下,我在itertools文档中找到了一个称为“成对”的函数。 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: 您的combinations方法是正确的,您只需要再次将每个组合的结果转换为字典即可:

import itertools

def pairwise(input):
    for values in input.itervalues():
        for pair in itertools.combinations(values.iteritems(), 2):
            yield dict(pair)

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()

list(pairwise(pleio))

Output: 输出:

>>> 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

Which outputs: 哪个输出:

>>> 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']}

If you are on Python 3, replace .itervalues() and .iteritems() by .values() and .items() respectively. 如果你在Python 3中,取代.itervalues().iteritems().values().items()分别。

If you want all pair combinations, you could probably use the following which is shorter, but I would not say this is more efficient. 如果您想要所有对组合,则可以使用以下较短的组合,但我不会说这样更有效。

[dict([(x,vx),(y,vy)]) for (x,vx) in pleio['firstLine'].iteritems()
                       for (y,vy) in pleio['firstLine'].iteritems()
                       if x < y]

Output 输出量

[{'enf3': ['34', 'set'], 'enf4': ['12', 'free']},
 {'enf2': ['48', 'free'], 'enf3': ['34', 'set']},
 {'enf2': ['48', 'free'], 'enf4': ['12', 'free']},
 {'enf1': ['54', 'set'], 'enf3': ['34', 'set']},
 {'enf1': ['54', 'set'], 'enf2': ['48', 'free']},
 {'enf1': ['54', 'set'], 'enf4': ['12', 'free']}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM