【问题标题】:Python: Generating multiple combinations of strings with spacesPython:生成带空格的字符串的多种组合
【发布时间】:2018-04-24 15:44:23
【问题描述】:

我有一个包含 n 个单词的数组,例如:

input: ["just", "a", "test"]

我需要做的是创建由空格分隔的这些单词的所有可能组合以及与原始字符串的组合。例如,上面应该创建:

output: [["just", "a", "test"], ["just a", "test"], ["just a test"], ["just", "a test"]]

我一直在使用 itertools,但无法让它完成我需要的工作。我现在拥有的:

iterable = ['just', 'a', 'test']

for n in chain.from_iterable(combinations(iterable, n) for n in range(len(iterable)+1)):
    print(n)

以下几乎可以按要求工作:

iterable = ['just', 'a', 'test']
L = [''.join(reversed(x)).rstrip()
     for x in product(*[(c, c+' ') for c in reversed(iterable)])]
print(L)

谢谢。

编辑:

为了阐明这对长度为 4 的数组应该如何工作: 输入:['an', 'even', 'bigger', 'test']`

output: 
['an', 'even', 'bigger', 'test']
['an even', 'bigger', 'test']
['an even bigger', 'test']
['an even bigger test']

['an', 'even bigger', 'test']
['an even', 'bigger test']
['an', 'even bigger test']
['an', 'even', 'bigger test']

【问题讨论】:

  • 我认为 Sage 库提供了一种从 input 计算您需要的非交叉分区集的方法。鉴于该集合intermediate,那么您只需要[map(" ".join, x) for x in intermediate]

标签: python string combinations whitespace permutation


【解决方案1】:

这是一种解决方案。 partitions 函数是courtesy of @Kiwi

from itertools import combinations

iterable = ['just', 'a', 'test', 'and', 'another']

n = len(iterable)

def partitions(items, k):

    def split(indices):
        i=0
        for j in indices:
            yield items[i:j]
            i = j
        yield items[i:]

    for indices in combinations(range(1, len(items)), k-1):
        yield list(split(indices))

for i in range(1, n+1):
    for x in partitions(iterable, i):
        print([' '.join(y) for y in x])

['just a test and another']
['just', 'a test and another']
['just a', 'test and another']
['just a test', 'and another']
['just a test and', 'another']
['just', 'a', 'test and another']
['just', 'a test', 'and another']
['just', 'a test and', 'another']
['just a', 'test', 'and another']
['just a', 'test and', 'another']
['just a test', 'and', 'another']
['just', 'a', 'test', 'and another']
['just', 'a', 'test and', 'another']
['just', 'a test', 'and', 'another']
['just a', 'test', 'and', 'another']
['just', 'a', 'test', 'and', 'another']        

【讨论】:

  • @JPMilward,啊,我明白了,好的。
  • 那是完美的。你是个天才。非常感谢。
【解决方案2】:

你可以试试这个(兼容 python 2.x 和 python 3.x):

s = ["this", "is", "just", "a", "simple", "test"] # the input
sepCount = len(s) - 1 # separator count of the input
output = [] # output

for i in range(0, 2 ** sepCount): # iterate through all possible combinations
    t = s # modified string
    j = i # for converting to binary
    for k in reversed(range(sepCount)):
        if j % 2 == 0:
            t = t[ : k] + [" ".join(t[k : k + 2])] + t [k + 2 :] # replace separator to " "
        j = j // 2
    output.append(t)

print(output)

输出:

[['this is just a simple test'],
['this is just a simple', 'test'],
['this is just a', 'simple test'],
['this is just a', 'simple', 'test'],
['this is just', 'a simple test'],
['this is just', 'a simple', 'test'],
['this is just', 'a', 'simple test'],
['this is just', 'a', 'simple', 'test'],
['this is', 'just a simple test'],
['this is', 'just a simple', 'test'],
['this is', 'just a', 'simple test'],
['this is', 'just a', 'simple', 'test'],
['this is', 'just', 'a simple test'],
['this is', 'just', 'a simple', 'test'],
['this is', 'just', 'a', 'simple test'],
['this is', 'just', 'a', 'simple', 'test'],
['this', 'is just a simple test'],
['this', 'is just a simple', 'test'],
['this', 'is just a', 'simple test'],
['this', 'is just a', 'simple', 'test'],
['this', 'is just', 'a simple test'],
['this', 'is just', 'a simple', 'test'],
['this', 'is just', 'a', 'simple test'],
['this', 'is just', 'a', 'simple', 'test'],
['this', 'is', 'just a simple test'],
['this', 'is', 'just a simple', 'test'],
['this', 'is', 'just a', 'simple test'],
['this', 'is', 'just a', 'simple', 'test'],
['this', 'is', 'just', 'a simple test'],
['this', 'is', 'just', 'a simple', 'test'],
['this', 'is', 'just', 'a', 'simple test'],
['this', 'is', 'just', 'a', 'simple', 'test']]

动机:长度为 n 的列表有 n-1 个分隔符 (,)。有 2^(n-1) 种方法可以用空格替换 ,s。通过迭代所有这 2^(n-1) 种可能的方式,您可以生成由空格分隔的这些单词的所有可能组合。

【讨论】:

  • 这也很有效。非常令人印象深刻,谢谢。
猜你喜欢
  • 1970-01-01
  • 2013-05-04
  • 2011-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多