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

Python 生成列表元素对的每个排列,而不重复或反转对,python,Python,我知道这与之前的问题类似,但我的要求有足够的差异,值得提出一个新问题。我有一个字符串元素列表 >>> mainlist ['one', 'two', 'three', 'four', 'five'] 我想创建一个循环程序,它接受第一个元素,然后将其与其余元素配对,如下所示: ['one two', 'one three', 'one four', 'one five'] 请注意,它没有创建一对'one' 下一个周期应为: ['two three', 'two, four',

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

>>> mainlist
['one', 'two', 'three', 'four', 'five']
我想创建一个循环程序,它接受第一个元素,然后将其与其余元素配对,如下所示:

['one two', 'one three', 'one four', 'one five']
请注意,它没有创建一对
'one'

下一个周期应为:

['two three', 'two, four', 'two five']
请再次注意,它并没有创建
'two-two'
,甚至也没有创建
'two-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...

基于以上,你可以看到,这并不完全符合我所追求的。任何帮助都将不胜感激-我确信在某个地方有一个优雅的解决方案。

只需使用嵌套的
进行
循环:

>>> mainlist = ['one', 'two', 'three', 'four', 'five']
>>> for e in mainlist:
    for i in mainlist:
        if e == i:
            print "Skipping"
        else:
            print e, i


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
>>> 
您要使用:

同样,您也可以通过将
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
two-two
等,则应使用


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


最后,如果要显式编写循环,可以使用
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
这基本上就是
组合所做的,除了它适用于任意大小(对、三元组等)

一种解决方案:

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所建议的那样,探索无限的可能性

输出

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

使用索引的嵌套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]

不幸的是,这会产生反向对,例如“一两个”和“两个一个”,所以不太好。谢谢-我将使用它,因为它在我现有的代码中最容易实现。谢谢,列表索引是我所需要的@比曼,不客气。如果你想奖励我,请随时将我的答案标记为“有用”或“已接受”。-)嗨,我必须将我的答案标记为已接受的答案-尽管你和其他人同样有用,对不起。此外,我没有足够的代表来标记你的帮助,但当我这样做的时候,我会
['one two', 'one three', 'one four', 'one five']
['two three', 'two four', 'two five']
['three four', 'three five']
['four five']
[]
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]