2016-04-26 49 views
0

我知道这与以前的问题类似,但我的请求中存在足够的差异以值得提出新问题。我有一个字符串元素的列表。生成列表元素对的每个置换而不重复或倒置对

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

基于上述可以看到,这不完全匹配我后。任何帮助将非常感激 - 我敢肯定,有一个优雅的解决方案在那里。

回答

0

嵌套使用指数应该做的伎俩循环:

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] 
+0

谢谢 - 我会使用它,因为它是在我现有的代码中最容易实现的 – Beeman

0

只需使用嵌套for循环:

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

这不幸产生倒对如。 '一个二'和'两个'如此不好。 – Beeman

7

你想用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 

如果你wan吨也产生对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')] 

最后,如果你想要写的循环明确可以使用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确实,除了它与任意大小(对,三胞胎等)

+0

辉煌 - 非常感谢您的解释 – Beeman

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

或者你可以探索的itertools的无限可能通过@Bakuriu的建议。

输出

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

谢谢,列表索引是我需要的! – Beeman

+0

@Beeman不客气。如果您想奖励我,请随时将我的答案标记为有用或被接受:-) – totoro

+0

嗨,我必须标记我接受的答案 - 尽管您和其他人同样有用,对不起。此外,我没有足够的代表来标记你的帮助,但是当我这样做时,我应该 – Beeman

相关问题