私はこれらのスクリプトの出力が矛盾している理由について熟考してきました。誰も私にこれを手伝ってもらえますか?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)
ありますラインアプローチ?
と
をあなたは、このようにnwise' 'に渡すリストに複数の単語を追加し、すべての反復にwords''に追加しています。 – Caramiriel
**ラムダ式**で 'i'は** **に限定されていないためです。 –
'map'の呼び出しではラムダ式は必要ありません。 'lambda x:" ".join(x)'は '' ".join'と同じように動作します。 – chepner