2016-04-26 21 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

相關問題