2016-04-26 15 views
2

Das heißt, für einen Satz brechen sie in alle möglichen Kombinationen von in Ordnung Worten zusammen, mit keinen WortenPython - erhalten alle um Splits eines Strings

Zum Beispiel verzichtet, für die Eingabe „Die Katze saß auf der Matte“

Ausgang

[("The", "cat sat on the mat"), 
("The cat", "sat on the mat"), 
("The cat", "sat", "on the mat")] #etc 

aber nicht

("The mat", "cat sat on the") # out of order 
("The cat"), ("mat") # words missing 

Ich habe Methoden in iwertools angeschaut, kann aber nicht sehen, dass sie die Aufgabe erfüllen, da Kombinationen Elemente vermissen ("die Katze", "Matte") und Permutationen die Reihenfolge ändern.

Fehle ich etwas in diesen Werkzeugen, oder sind sie einfach nicht richtig?

(Aus Gründen der Klarheit ist dies nicht eine Frage, wie eine Zeichenfolge zu spalten, aber wie Kombinationen zu bekommen)

+0

Hallo, begrüßen SO. Nur um es zu verdeutlichen, fangen wir an mit "Die Katze saß auf der Matte" .split ('') ', die '[' The ',' cat ',' sat ',' on ',' the ', 'mat'] '? –

+0

Ja, mit einer Liste, bereits geteilt – havlock

+0

OK, und würde '(" Die Katze "," saß "," auf "," die Matte ")' auch eine Ausgabe, die Sie erwarten? –

Antwort

1

Raymond Hettinger's partition recipe für Python ändern 3, wie durch this blog post von WordAligned inspiriert, und jede Partition Fall mit Ihre Liste können wir mit chain und combinations von itertools erreichen.

from itertools import chain, combinations 
def partition(iterable): 
    n = len(input_list) 
    b, mid, e = [0], list(range(1, n)), [n] 
    getslice = input_list.__getitem__ 
    splits = (d for i in range(n) for d in combinations(mid, i)) 
    return [[input_list[sl] for sl in map(slice, chain(b, d), chain(d, e))] 
      for d in splits] 

Demo:

>>> print(partition(input_list)) 
[[['The', 'cat', 'sat', 'on', 'the', 'mat']], [['The'], ['cat', 'sat', 'on', 'the', 'mat']], [['The', 'cat'], ['sat', 'on', 'the', 'mat']], [['The', 'cat', 'sat'], ['on', 'the', 'mat']], [['The', 'cat', 'sat', 'on'], ['the', 'mat']], [['The', 'cat', 'sat', 'on', 'the'], ['mat']], [['The'], ['cat'], ['sat', 'on', 'the', 'mat']], [['The'], ['cat', 'sat'], ['on', 'the', 'mat']], [['The'], ['cat', 'sat', 'on'], ['the', 'mat']], [['The'], ['cat', 'sat', 'on', 'the'], ['mat']], [['The', 'cat'], ['sat'], ['on', 'the', 'mat']], [['The', 'cat'], ['sat', 'on'], ['the', 'mat']], [['The', 'cat'], ['sat', 'on', 'the'], ['mat']], [['The', 'cat', 'sat'], ['on'], ['the', 'mat']], [['The', 'cat', 'sat'], ['on', 'the'], ['mat']], [['The', 'cat', 'sat', 'on'], ['the'], ['mat']], [['The'], ['cat'], ['sat'], ['on', 'the', 'mat']], [['The'], ['cat'], ['sat', 'on'], ['the', 'mat']], [['The'], ['cat'], ['sat', 'on', 'the'], ['mat']], [['The'], ['cat', 'sat'], ['on'], ['the', 'mat']], [['The'], ['cat', 'sat'], ['on', 'the'], ['mat']], [['The'], ['cat', 'sat', 'on'], ['the'], ['mat']], [['The', 'cat'], ['sat'], ['on'], ['the', 'mat']], [['The', 'cat'], ['sat'], ['on', 'the'], ['mat']], [['The', 'cat'], ['sat', 'on'], ['the'], ['mat']], [['The', 'cat', 'sat'], ['on'], ['the'], ['mat']], [['The'], ['cat'], ['sat'], ['on'], ['the', 'mat']], [['The'], ['cat'], ['sat'], ['on', 'the'], ['mat']], [['The'], ['cat'], ['sat', 'on'], ['the'], ['mat']], [['The'], ['cat', 'sat'], ['on'], ['the'], ['mat']], [['The', 'cat'], ['sat'], ['on'], ['the'], ['mat']], [['The'], ['cat'], ['sat'], ['on'], ['the'], ['mat']]] 
+0

Ausgezeichnet! Vielen Dank. (Ich denke, das Argument für die Partition sollte "input_list" sein, nicht "iterable", oder? Ich vermisse etwas?) – havlock

+0

Es könnte geändert werden, ja. Ich habe nur iteriert, weil diese Funktion nicht mein eigener Code ist. – miradulo

Verwandte Themen