[英]How to identify ordered substrings in string using python?
I am new to python, so my question would be naive but I would appreciate your suggestion that help me get to the solution of it. 我是python的新手,所以我的问题会很幼稚,但是我很感谢您的建议,可以帮助我解决它。
For example I have string "aeshfytifghkjgiomntrop" and I want to find ordered substrings from it. 例如,我有字符串“ aeshfytifghkjgiomntrop”,我想从中找到有序的子字符串。 How should I approach?
我应该如何处理?
Scan the string using enumerate
to issue index and value. 使用
enumerate
扫描字符串以发布索引和值。 Print string parts when chars are decreasing and start a new substring: 当字符减少时打印字符串部分,并开始一个新的子字符串:
s = "aeshfytifghkjgiomntrop"
prev_c = None
prev_i = 0
for i,c in enumerate(s):
if prev_c>c:
print(s[prev_i:i])
prev_i=i
prev_c=c
print(s[prev_i:])
result: 结果:
aes
h
fy
t
i
fghk
j
gio
mnt
r
op
(No particular Python magic here, could be done simply even with C) (这里没有特殊的Python魔术,即使使用C也可以轻松完成)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.