2017-10-25 15 views
0

私はこれらのスクリプトの出力が矛盾している理由について熟考してきました。誰も私にこれを手伝ってもらえますか?Python itertools関数:矛盾した出力

import itertools 
from itertools import tee 
from itertools import islice 

words = ['Salad','with','Chocolate','and','potatoes'] 
nwise = lambda xs,n=2: zip(*(islice(xs,idx,None) for idx,xs in enumerate(tee(xs,n)))) 
doubles = list(map(lambda x: " ".join(x), nwise(words,2))) 
triples = list(map(lambda x: " ".join(x), nwise(words,3))) 
quadrouples = list(map(lambda x: " ".join(x), nwise(words,4))) 
words.extend(doubles) 
words.extend(triples) 
words.extend(quadrouples) 
print(words) 

この結果は、この結果は、なぜ範囲()関数でforループ線バイへの一貫性のない結果を生成しない['Salad', 'with', 'Chocolate', 'and', 'potatoes', 'Salad with', 'with Chocolate', 'Chocolate and', 'and potatoes', 'Salad with Chocolate', 'with Chocolate and', 'Chocolate and potatoes', 'and potatoes Salad with', 'potatoes Salad with with Chocolate', 'Salad with with Chocolate Chocolate and', 'with Chocolate Chocolate and and potatoes', 'Salad with Chocolate and', 'with Chocolate and potatoes', 'Chocolate and potatoes Salad with', 'and potatoes Salad with with Chocolate', 'potatoes Salad with with Chocolate Chocolate and', 'Salad with with Chocolate Chocolate and and potatoes', 'with Chocolate Chocolate and and potatoes Salad with Chocolate', 'Chocolate and and potatoes Salad with Chocolate with Chocolate and', 'and potatoes Salad with Chocolate with Chocolate and Chocolate and potatoes', 'Salad with Chocolate with Chocolate and Chocolate and potatoes and potatoes Salad with', 'with Chocolate and Chocolate and potatoes and potatoes Salad with potatoes Salad with with Chocolate', 'Chocolate and potatoes and potatoes Salad with potatoes Salad with with Chocolate Salad with with Chocolate Chocolate and', 'and potatoes Salad with potatoes Salad with with Chocolate Salad with with Chocolate Chocolate and with Chocolate Chocolate and and potatoes']

ある['Salad', 'with', 'Chocolate', 'and', 'potatoes', 'Salad with', 'with Chocolate', 'Chocolate and', 'and potatoes', 'Salad with Chocolate', 'with Chocolate and', 'Chocolate and potatoes', 'Salad with Chocolate and', 'with Chocolate and potatoes']

import itertools 
from itertools import tee 
from itertools import islice 

words = ['Salad','with','Chocolate','and','potatoes'] 
nwise = lambda xs,n=2: zip(*(islice(xs,idx,None) for idx,xs in enumerate(tee(xs,n)))) 
for i in range(2,5): 
    new = list(map(lambda x: " ".join(x), nwise(words,i))) 
    words.extend(new) 
print(words) 

ありますラインアプローチ?

+1

... for i in range(2,5): new = list(map(lambda x: " ".join(x), nwise(words,i))) words.extend(new) ... 

をあなたは、このようにnwise' 'に渡すリストに複数の単語を追加し、すべての反復にwords''に追加しています。 – Caramiriel

+0

**ラムダ式**で 'i'は** **に限定されていないためです。 –

+2

'map'の呼び出しではラムダ式は必要ありません。 'lambda x:" ".join(x)'は '' ".join'と同じように動作します。 – chepner

答えて

2

修正するためにループするときは、一般にcreate a new collectionより安全です。

新しいリストに拡張します。 words_

あるいは
... 
words_ = [] 
for i in range(2, 5): 
    new = list(map(lambda x: " ".join(x), nwise(words,i))) 
    words_.extend(new) 
print(words_) 

、所望の結果のnewリストと既存のwordsリストを拡張します。

は交換してください:

... 
new = [" ".join(x) for i in range(2, 5) for x in nwise(words,i)] 
words.extend(new) 
... 
+0

これは論理エラーの原因ですが、ループ内でオブジェクトを反復しながらオブジェクトを反復処理する通常の場合(突然変異と短縮ループが発生する可能性があります)と同じように、突然変異と反復が混在することはありません。この繰り返しは範囲を超えていますが、mutationsは 'words'に適用されます(これは各ループの入出力ですが、ループが直接反復する値ではありません)。あなたの解決策は素晴らしいです(すべての 'nwise'仕事が終わるまで、' words'への変更は延期されますが)、説明はオフです。 *インテント*が以前の出力に基づいて構築されていれば、このコードについては不快感はありません。 – ShadowRanger

+0

はい私は文言が誤解されることがある方法を参照してください。私は編集します。ありがとう。 – pylang

+0

ありがとう@pylang - 魅力のように動作します! – LukasKlement