python 在python中查找子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/788699/
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
finding substrings in python
提问by Alex Martelli
Can you please help me to get the substrings between two characters at each occurrence
你能帮我在每次出现时获取两个字符之间的子字符串吗
For example to get all the substrings between "Q" and "E" in the given example sequence in all occurrences:
例如,要获取给定示例序列中所有出现的“Q”和“E”之间的所有子字符串:
ex: QUWESEADFQDFSAEDFS
and to find the substring with minimum length.
并找到长度最小的子串。
回答by RichieHindle
import re
DATA = "QUWESEADFQDFSAEDFS"
# Get all the substrings between Q and E:
substrings = re.findall(r'Q([^E]+)E', DATA)
print "Substrings:", substrings
# Sort by length, then the first one is the shortest:
substrings.sort(key=lambda s: len(s))
print "Shortest substring:", substrings[0]
回答by Alex Martelli
RichieHindle has it right, except that
RichieHindle 说得对,除了
substrings.sort(key=len)
is a better way to express it than that redundant lambda;-).
是一种比冗余 lambda 更好的表达方式;-)。
If you're using Python 2.5 or later, min(substrings, key=len) will actually give you the one shortest string (the first one, if several strings tie for "shortest") quite a bit faster than sorting and taking the [0]th element, of course. But if you're stuck with 2.4 or earlier, RichieHindle's approach is the best alternative.
如果您使用的是 Python 2.5 或更高版本, min(substrings, key=len) 实际上会给您一个最短的字符串(第一个,如果多个字符串并列“最短”)比排序和取 [ 0]th 元素,当然。但如果您坚持使用 2.4 或更早版本,RichieHindle 的方法是最佳选择。