获取字符串及其子字符串的所有组合 [英] Getting all combinations of a string and its substrings

查看:39
本文介绍了获取字符串及其子字符串的所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多关于获取所有可能的子字符串(即相邻的字符集)的问题,但没有关于生成所有可能的字符串(包括其子字符串的组合)的问题.

例如,让:

x = 'abc'

我希望输出类似于:

['abc', 'ab', 'ac', 'bc', 'a', 'b', 'c']

重点是我们可以去除原始字符串中不相邻的多个字符(以及相邻的字符).

这是我迄今为止尝试过的:

def return_substrings(input_string):长度 = len(input_string)返回 [input_string[i:j + 1] for i in range(length) for j in range(i, length)]打印(return_substrings('abc'))

然而,这只会从原始字符串中删除相邻字符串的集合,不会返回上例中的元素 'ac'.

另一个例子是如果我们使用字符串 'abcde',输出列表应该包含元素 'ace', 'bd'

解决方案

您可以使用 itertools.combinations

<预><代码>>>>从 itertools 导入组合>>>x = 'abc'>>>[''.join(l) for i in range(len(x)) for l in组合(x, i+1)]['a', 'b', 'c', 'ab', 'ac', 'bc', 'abc']

如果你想以相反的顺序,你可以让range函数以相反的顺序返回它的序列

<预><代码>>>>[''.join(l) for i in range(len(x),0,-1) for l in组合(x, i)]['abc', 'ab', 'ac', 'bc', 'a', 'b', 'c']

I've seen many questions on getting all the possible substrings (i.e., adjacent sets of characters), but none on generating all possible strings including the combinations of its substrings.

For example, let:

x = 'abc'

I would like the output to be something like:

['abc', 'ab', 'ac', 'bc', 'a', 'b', 'c']

The main point is that we can remove multiple characters that are not adjacent in the original string (as well as the adjacent ones).

Here is what I have tried so far:

def return_substrings(input_string):
    length = len(input_string)
    return [input_string[i:j + 1] for i in range(length) for j in range(i, length)]

print(return_substrings('abc'))

However, this only removes sets of adjacent strings from the original string, and will not return the element 'ac' from the example above.

Another example is if we use the string 'abcde', the output list should contain the elements 'ace', 'bd' etc.

解决方案

You can do this easily using itertools.combinations

>>> from itertools import combinations
>>> x = 'abc'
>>> [''.join(l) for i in range(len(x)) for l in combinations(x, i+1)]
['a', 'b', 'c', 'ab', 'ac', 'bc', 'abc']

If you want it in the reversed order, you can make the range function return its sequence in reversed order

>>> [''.join(l) for i in range(len(x),0,-1) for l in combinations(x, i)]
['abc', 'ab', 'ac', 'bc', 'a', 'b', 'c']

这篇关于获取字符串及其子字符串的所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆