Ich habe darüber nachgedacht, warum die Ausgabe dieser Skripte inkonsistent ist. Könnte mir jemand dabei helfen?Python itertools Funktion: inkonsistente Ausgabe
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)
Das Ergebnis hiervon ist ['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)
Das Ergebnis hiervon ist ['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']
Warum bei dem Bereich() -Funktion ist die for-Schleife inkonsistente Ergebnisse der produzieren Linie-für- Linienansatz?
Sie auf 'words' bei jeder Iteration sind anhängt, so dass mehr Wörter in die Liste zu' nwise' zu übergeben. – Caramiriel
Weil das 'i' im ** Lambda-Ausdruck ** nicht ** gebunden ist **. –
Sie brauchen keinen Lambda-Ausdruck in den Aufrufen von 'map'; 'Lambda x:" ".join (x)' verhält sich genauso wie '" ".join'. – chepner