2016-10-11 13 views
0

私の出力の重要な部分は、finalListの長さを識別することが、どこかに私のコードでは、重複が削除されることができることであり、私は私のコードで重複が削除されていますか?

from itertools import chain, permutations 
allPos = [] 
first_list = ['a','b','c'] 
match_list = [['a','b','c'], ['a','b','c']] 

for i in range(1,30): 
    for phrase in permutations(first_list, i): 
     for ind, letter in enumerate(chain.from_iterable(phrase)): 
      if ind >= len(match_list) or letter not in match_list[ind]: 
       break 
     else: 
      allPos.append(phrase) 

finalList = [] 

for i in allPos: 
    if len(i) == len(allPos[-1]): 
     finalList.append(i) 

print(finalList) 

OUTPUT

[('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')] 
を把握することはできません

私はそれが重複を削除していることを知っている、またはおそらく私のコードが何かを完全に欠落している私の出力から[('a','a'), ('b','b'), ('c','c')]

+5

ここでは:['permutations'](https://docs.python.org/2/library/itertools.html#itertools.permutations)? – zvone

+0

はあなたの他のブロックがインデントされているはずですか? – kpie

+0

私は文書を読んでいます – oneman

答えて

1

あなたがこれを試すことができます。順列を使用してiterableを変更します。

from itertools import chain, permutations 
... 
... 
for i in range(1,30): 
    # change iterable 
    for phrase in permutations([j for ele in match_list for j in ele], i): 

... 
for i in set(allPos): 
    if len(i) == len(allPos[-1]): 
     finalList.append(i) 

print (sorted(finalList)) 
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')] 
関連する問題