How to return alphabetical substrings?

I am trying to write a function that takes a string sas input and returns a list of substrings inside sthat are alphabetical. For example, s = 'acegibdh'should return ['acegi', 'bdh'].

Here is the code I came up with:

s = 'acegibdh'
ans = []
subs = []
i = 0
while i != len(s) - 1:
    while s[i] < s[i+1]:
        subs.append(s[i])
        i += 1
    if s[i] > s[i-1]:
        subs.append(s[i])
        i += 1
    subs = ''.join(subs)
    ans.append(subs)
    subs = []
print ans 

He is having problems with the last letter of the string, because test + 1 is outside the range of indices. I spent a lot of time with him to try to find a way to avoid this problem. Does anyone know how to do this?

+4
source share
7 answers

ans, ? , .

>>> s = 'acegibdh'
>>> ans = []
>>> ans.append(s[0])
>>> for letter in s[1:]:
...     if letter >= ans[-1][-1]:
...             ans[-1] += letter
...     else:
...             ans.append(letter)
...
>>> ans
['acegi', 'bdh']
+4
s = 'acegibdh'
ans = []
subs = []
subs.append(s[0])
for x in range(len(s)-1):
    if s[x] <= s[x+1]:
        subs.append(s[x+1])
    if s[x] > s[x+1]:
        subs = ''.join(subs)
        ans.append(subs)
        subs = []
        subs.append(s[x+1])
subs = ''.join(subs)
ans.append(subs)
print ans 

, - .

+1

, .

>>> s='acegibdh'
>>> [s[l:r] for l,r in (lambda seq:zip(seq,seq[1:]))([0]+[idx+1 for idx in range(len(s)-1) if s[idx]>s[idx+1]]+[len(s)])]
['acegi', 'bdh']
+1

, . , . , .

s = 'acegibdh'
ans = []
current = str(s[0])
i = 1
while i < len(s):
    if s[i] > s[i-1]:
        current += s[i]
    else: 
        ans.append(current)
        current = ''
    i += 1
if current != '':
   ans.append(current)
print ans 
0

, char .

:

s = 'acegibdh'
prev = None
ans = []
subs = []
for ch in s:
    if prev is None or ch > prev:
        subs.append(ch)
    else:
        ans.append(''.join(subs))
        subs = [ch]
    prev = ch
ans.append(''.join(subs))

, ( - char append, ). .

0

, - -

from itertools import groupby,chain,cycle

def my_gen(s):
    check = cycle([1,0])
    for k,v in groupby(zip(s,s[1:]),lambda x:x[0]<x[1]):
        if k:
            v = zip(*v)
            yield v[0] + (v[1][-1],)

print list(my_gen('acegibdhabcdefghijk'))
0

.

, i s[i:j] s[j] < s[j-1], i j.

, , :

def alpha_subs(s):
    i, j = 0, 1
    while j < len(s):
        if s[j] < s[j-1]:
            yield s[i:j]
            i = j
        j += 1
    if s[i:j]:
        yield s[i:j]

print(list(alpha_subs('')))
print(list(alpha_subs('acegibdh')))
print(list(alpha_subs('acegibdha')))

[]
['acegi', 'bdh']
['acegi', 'bdh', 'a']

For case insensitivity:

def alpha_subs(s, ignore_case=False):
    qs = s.lower() if ignore_case else s
    i, j = 0, 1
    while j < len(s):
        if qs[j] < qs[j-1]:
            yield s[i:j]
            i = j
        j += 1
    if s[i:j]:
        yield s[i:j]

print(list(alpha_subs('acEgibDh', True)))
print(list(alpha_subs('acEgibDh')))

['acEgi', 'bDh']
['ac', 'Egi', 'b', 'Dh']
0
source

All Articles