2017-10-25 5 views
0

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?

+1

Sie auf 'words' bei jeder Iteration sind anhängt, so dass mehr Wörter in die Liste zu' nwise' zu ​​übergeben. – Caramiriel

+0

Weil das 'i' im ** Lambda-Ausdruck ** nicht ** gebunden ist **. –

+2

Sie brauchen keinen Lambda-Ausdruck in den Aufrufen von 'map'; 'Lambda x:" ".join (x)' verhält sich genauso wie '" ".join'. – chepner

Antwort

2

Es ist in der Regel sicherer, create a new collection beim Schleifen zu ändern.

Erweitern Sie eine neue Liste, z. words_:

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

Alternativ erstrecken sich die vorhandene words Liste mit einer Liste der new gewünschten Ergebnisse.

ersetzen:

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

mit

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

Während es die Ursache für den logischen Fehler ist, sind die Mutation und die Iteration nicht wie im normalen Fall, bei dem Sie ein Objekt iterieren, während es innerhalb der Schleife mutiert (was zu unendlichen oder verkürzten Schleifen führen kann), vermischt. Diese Iteration liegt über einem Bereich, während die Mutation auf "Wörter" angewendet wird (dies ist eine Eingabe/Ausgabe in jeder Schleife, aber nicht der Wert, den die Schleife direkt iteriert). Deine Lösung ist in Ordnung (verschiebe Änderungen an "Wörtern", bis nach all 'nweiser' Arbeit), aber die Erklärung ist aus; Es gäbe nichts unpythonisches an diesem Code, wenn die * Absicht * auf den vorherigen Ausgaben aufbauen sollte. – ShadowRanger

+0

Ja, ich sehe, wie der Wortlaut falsch interpretiert werden kann. Ich werde bearbeiten. Vielen Dank. – pylang

+0

Dank @pylang - funktioniert wie ein Charme! – LukasKlement