如何在 Python 中获取一个字符串的所有连续子字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22469997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:00:12  来源:igfitidea点击:

How To Get All The Contiguous Substrings Of A String In Python?

pythonstringpython-2.7substring

提问by lqhcpsgbl

Here is my code, but I want a better solution, how do you think about the problem?

这是我的代码,但我想要一个更好的解决方案,你如何看待这个问题?

def get_all_substrings(string):
  length = len(string)
  alist = []
  for i in xrange(length):
    for j in xrange(i,length):
      alist.append(string[i:j + 1]) 
  return alist

print get_all_substring('abcde')

采纳答案by thefourtheye

The only improvement I could think of is, to use list comprehension like this

我能想到的唯一改进是,像这样使用列表理解

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

print get_all_substrings('abcde')

The timing comparison between, yours and mine

你和我的时间比较

def get_all_substrings(string):
  length = len(string)
  alist = []
  for i in xrange(length):
    for j in xrange(i,length):
      alist.append(string[i:j + 1]) 
  return alist

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

from timeit import timeit
print timeit("get_all_substrings('abcde')", "from __main__ import get_all_substrings")
# 3.33308315277
print timeit("get_all_substrings_1('abcde')", "from __main__ import get_all_substrings_1")
# 2.67816185951

回答by John La Rooy

You could write it as a generator to save storing all the strings in memory at once if you don't need to

如果您不需要,您可以将其编写为生成器以将所有字符串一次保存在内存中

def get_all_substrings(string):
    length = len(string)
    for i in xrange(length):
        for j in xrange(i + 1, length + 1):
            yield(string[i:j]) 

for i in get_all_substrings("abcde"):
    print i

you can still make a list if you really need one

如果你真的需要,你仍然可以列一个清单

alist = list(get_all_substrings("abcde"))

The function can be reduced to return a generator expression

该函数可以简化为返回一个生成器表达式

def get_all_substrings(s):
    length = len(s)
    return (s[i: j] for i in xrange(length) for j in xrange(i + 1, length + 1))

Or of course you can change two characters to return a list if you don't care about memory

或者当然你可以改变两个字符来返回一个列表,如果你不关心内存

def get_all_substrings(s):
    length = len(s)
    return [s[i: j] for i in xrange(length) for j in xrange(i + 1, length + 1)]

回答by sanooj

Another solution:

另一种解决方案:

def get_all_substrings(string):
   length = len(string)+1
   return [string[x:y] for x in range(length) for y in range(length) if string[x:y]]

print get_all_substring('abcde')

回答by PaulMcG

I've never been fond of range(len(seq)), how about using enumerate and just using the index value:

我从来没有喜欢过range(len(seq)),如何使用 enumerate 并仅使用索引值:

def indexes(seq, start=0):
    return (i for i,_ in enumerate(seq, start=start))

def gen_all_substrings(s):
    return (s[i:j] for i in indexes(s) for j in indexes(s[i:], i+1))

def get_all_substrings(string):
    return list(gen_all_substrings(string))

print(get_all_substrings('abcde'))

回答by PaulMcG

Use itertools.permutationsto generate all pairs of possible start and end indexes, and filter out only those where the start index is less than then end index. Then use these pairs to return slices of the original string.

使用itertools.permutations产生的所有对可能的开始和结束的指标,并过滤出那些开始指数小于然后结束索引。然后使用这些对返回原始字符串的切片。

from itertools import permutations

def gen_all_substrings(s):
    lt = lambda pair: pair[0] < pair[1]
    index_pairs = filter(lt, permutations(range(len(s)+1), 2))
    return (s[i:j] for i,j in index_pairs)

def get_all_substrings(s):
    return list(gen_all_substrings(s))

print(get_all_substrings('abcde'))

回答by PaulMcG

Python 3

蟒蛇 3

s='abc'
list(s[i:j+1] for i in range (len(s)) for j in range(i,len(s)))

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

回答by DyRuss

can be done concisely with itertools.combinations

可以简洁地完成 itertools.combinations

from itertools import combinations

def get_all_substrings_2(string):
    length = len(string) + 1
    return [string[x:y] for x, y in combinations(range(length), r=2)]

回答by Nkromin989

Another solution using 2-D matrix approach

另一种使用二维矩阵方法的解决方案

p = "abc"
a = list(p)
b = list(p)
c = list(p)
count = 0
for i in range(0,len(a)):
       dump = a[i]
            for j in range(0, len(b)):
                if i < j:
                    c.append(dump+b[j])
                    dump = dump + b[j]