2016-04-26 5 views
2

の順分割にすべてを取得する言葉を入力 のために例えばパイソン - 、ある文字列

を省略しないで文のために、インオーダーの単語のすべての可能な組み合わせにそれを破る「猫が座っていました「マットの上に

出力

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

なく

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

私はitertoolsのメソッドを見たが、組み合わせがアイテムを逃してしまう( "猫"、 "マット")、並べ替えが順序を変えるので、彼らは仕事をしているのを見ることができない。

私はこれらのツールで何かを見逃していますか、それとも正しいものではありませんか?

(分かりやすくするために、これは文字列を分割する方法についての質問ではなく、組み合わせを取得する方法)

+0

こんにちは、SOへようこそ。明確にするために、私たちは ''猫がマットに座っていたから始まります。 '' '' ''、 'cat'、 'sat'、 'on'、 'the' 'マット'] '? –

+0

はい、リストで、既に分割されています – havlock

+0

OK、 '(" The cat "、" sat "、" on "、" the mat ")'も期待される出力ですか? –

答えて

1

WordAlignedからthis blog postに触発としてPython 3用Raymond Hettinger's partition recipeの変更、および持つすべてのパーティションケースあなたのリストは、itertoolsのchaincombinationsでこれを達成できます。

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] 

デモ

>>> 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

優秀!ありがとう。 (私はパーティションへの引数は "iterable"ではなく "input_list"でなければならないと思っていますか?あるいは、私はsometningが欠けていますか?) – havlock

+0

これは可能です。私はちょうどその関数が自分のコードではないので、繰り返し実行することができました。 – miradulo

関連する問題