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

Bee*_*man 3 python

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

>>> mainlist
['one', 'two', 'three', 'four', 'five']
Run Code Online (Sandbox Code Playgroud)

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

['one two', 'one three', 'one four', 'one five']
Run Code Online (Sandbox Code Playgroud)

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

下一个周期应该是:

['two three', 'two, four', 'two five']
Run Code Online (Sandbox Code Playgroud)

请注意,它再次没有创建'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...
Run Code Online (Sandbox Code Playgroud)

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

Bak*_*riu 11

你想用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
Run Code Online (Sandbox Code Playgroud)

以同样的方式,您还可以通过指定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
Run Code Online (Sandbox Code Playgroud)

如果你想也产生对one one,two 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')]
Run Code Online (Sandbox Code Playgroud)

最后,如果要显式编写循环,可以使用它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
Run Code Online (Sandbox Code Playgroud)

这基本上是combinations做什么的,除了它适用于任意大小(对,三胞胎等)


pls*_*ban 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]
Run Code Online (Sandbox Code Playgroud)