生成列表元素对的每个排列,而不重复或反对

时间:2016-04-26 07:37:53

标签: python

我知道这与以前的问题类似,但我的请求中有足够的差异值得提出新问题。我有一个字符串元素列表。

>>> mainlist
['one', 'two', 'three', 'four', 'five']

我想创建一个循环程序,它接受第一个元素,然后将其与其余元素配对,如下所示:

['one two', 'one three', 'one four', 'one five']

请注意,它没有创建货币对'one one'

下一个周期应该是:

['two three', 'two, four', 'two five']

请注意,它没有创建'two two'甚至'two one',因为我的目的是等于'one two'

依旧......

我最近的是:

for primary in mainlist:
    for secondary in mainlist:
        if primary == secondary: print("skipping...")
        else: print(primary + " " + secondary)


>> skipping...
one two
one three
one four
one five
two one
skipping...
two three
two four
two five
three one
three two
skipping...
three four
three five
four one
four two
four three
skipping...
four five
five one
five two
five three
five four
skipping...

基于以上所述,您可以看到这与我追求的完全不符。任何帮助都会非常感激 - 我确信那里有一个优雅的解决方案。

4 个答案:

答案 0 :(得分:9)

您想使用itertools.combinations

In [1]: import itertools as it

In [2]:  mainlist = ['one', 'two', 'three', 'four', 'five']

In [3]: for a,b in it.combinations(mainlist, 2):
   ...:     print(a, b)
   ...:     
one two
one three
one four
one five
two three
two four
two five
three four
three five
four five

同样,您也可以通过将3指定为第二个参数来创建所有可能的三元组:

In [4]: for a,b,c in it.combinations(mainlist, 3):
   ...:     print(a, b,c)
   ...:     
one two three
one two four
one two five
one three four
one three five
one four five
two three four
two three five
two four five
three four five

如果您还要生成one onetwo two等对,则应使用combinations_with_replacement代替。

如果要将具有相同第一个元素的对组合在一起,可以使用itertools.groupby

In [1]: import itertools as it
   ...: mainlist = ['one', 'two', 'three', 'four', 'five']
   ...: 

In [2]: for key, group in it.groupby(it.combinations(mainlist, 2), key=lambda x:x[0]):
   ...:     print('key is', key)
   ...:     print('grouped elements', list(group))
key is one
grouped elements [('one', 'two'), ('one', 'three'), ('one', 'four'), ('one', 'five')]
key is two
grouped elements [('two', 'three'), ('two', 'four'), ('two', 'five')]
key is three
grouped elements [('three', 'four'), ('three', 'five')]
key is four
grouped elements [('four', 'five')]

最后,如果您想明确编写循环,可以使用enumerate来跟踪当前索引:

In [3]: for i, el in enumerate(mainlist):
   ...:     for el2 in mainlist[i+1:]:
   ...:         print(el, el2)
   ...:         
one two
one three
one four
one five
two three
two four
two five
three four
three five
four five

这基本上是combinations的作用,除了它适用于任意大小(对,三元组等)

答案 1 :(得分:0)

只需使用嵌套的function goBack(event){ event.preventDefault(); window.history.back(); } 循环:

for

答案 2 :(得分:0)

一个解决方案:

l = ['one', 'two', 'three', 'four', 'five']

for i in range(len(l)):
    print ["{} {}".format(l[i], l[j]) for j in range(i + 1, len(l))]

或者你可以按照@Bakuriu的建议探索itertools的无限可能性。

<强>输出

['one two', 'one three', 'one four', 'one five']
['two three', 'two four', 'two five']
['three four', 'three five']
['four five']
[]

答案 3 :(得分:0)

使用索引嵌套for循环应该可以解决问题:

for i in range(len(mainlist)):
    for j in range(i,len(mainlist)):
        if mainlist[j] == mainlist[i]:
            print 'Skipping'
        else:
            print mainlist[i] + ' ' + mainlist[j]