简体   繁体   English

如何使用python获取子字符串

[英]How to get substrings using python

Suppose, I have a string name = 'baceb'假设,我有一个字符串name = 'baceb'
I want to store its substrings into a list.我想将它的子字符串存储到一个列表中。
For baceb the substrings will be like - "b, ba, bac, bace, a, ac, ace, aceb, c, ce, ceb, e, eb, baceb"对于baceb ,子字符串将类似于 - "b, ba, bac, bace, a, ac, ace, aceb, c, ce, ceb, e, eb, baceb"
How can I get this substrings easily?我怎样才能轻松获得这个子字符串?

You can generate a list of all the substrings with a simple nested list comprehension:您可以使用简单的嵌套列表解析生成所有子字符串的列表:

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

This will however give you a repeated value, b , which occurs as both the first and last substring:然而,这会给你一个重复的值b ,它作为第一个和最后一个子字符串出现:

['b', 'ba', 'bac', 'bace', 'baceb', 'a', 'ac', 'ace', 'aceb', 'c', 'ce', 'ceb', 'e', 'eb', 'b']

If you don't want any duplicates, and you don't care about ordering, use a set comprehension instead and then convert to a list:如果您不想要任何重复项,并且您不关心排序,请改用集合推导式,然后转换为列表:

subs = list({ s[i:j+1] for i in range(len(s)) for j in range(i,len(s)) })

Output:输出:

['e', 'ba', 'a', 'aceb', 'c', 'ceb', 'eb', 'bac', 'baceb', 'bace', 'ce', 'ac', 'b', 'ace']

If you do care about ordering, there are many good solutions here that describe how to remove duplicates from a list while preserving ordering.如果您确实关心排序,这里有许多很好的解决方案它们描述了如何在保留排序的同时从列表中删除重复项。

Try this:尝试这个:

import itertools

s = 'baceb'
lst = [[s[i:j+1] for j in range(i,len(s))] for i in range(len(s))]
ss = set(itertools.chain.from_iterable(lst))
print(ss)

Output输出

{'e', 'ba', 'a', 'aceb', 'b', 'ace', 'bac', 'bace', 'ce', 'c', 'baceb', 'ceb', 'eb', 'ac'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM