简体   繁体   中英

python - remove a number of characters corresponding to the digit in string

I'm trying to write a function that returns a string that is similar to the consumed string s, but every time a digit appears in s, a number of characters corresponding to that digit, including the digit itself, should be removed from the string. If removing one digit's substring removes another digit, then the second digit shouldn't be considered. For example:

cleanstr("23apple") -> "apple"
cleanstr("100") -> "00"
cleanstr("6Hello") -> ""
cleanstr("0 red 37 blue") -> "0 red blue"

My code doesn't return the expected result when there are consecutive digits in the string. For example, cleanstr("01234560") returns "0260" instead of "0". So I know the problem in my loop is that after checking "0" and "1" it jumps over "2" and moves to "3". Can someone help me fix the problem?

def cleanstr(s):
    i = 0
    lst = list(s)
    while i < len(lst):
        if lst[i].isdigit():
            lst = lst[0:i] + lst[i+int(lst[i]):]
        i = i + 1
    return ''.join(lst)

It seems easier to think about this as either appending the current character or advancing the counter by i in each iteration:

def cleanstr(s):
    i = 0
    res = ''
    while i < len(s):
        if s[i].isdigit() and int(s[i]) > 0:
            i += int(s[i])
        else:
            res += s[i]
            i += 1
    return res

cleanstr("0 red 37 blue")
# '0 red blue'

cleanstr("23apple") 
# 'apple'

cleanstr("01234560") 
# '0'

You can create an iterator from the string to iterate through the characters, and output the character only if it isn't a digit between 1 and 9, or else consume the given number of characters minus 1 from the iterator with itertools.islice :

from itertools import islice
def cleanstr(s):
    i = iter(s)
    return ''.join(c for c in i if not '0' < c <= '9' or not (tuple(islice(i, int(c) - 1)),))
TESTED = ["23apple", "100", "6Hello", "0 red 37 blue"]

def cleanstr(s):
    i = 0
    while i<len(s):
        n = ord(s[i]) - ord('0')
        if n>=0 and n<=9:
            s = s[:i] + s[i+n:]
            i = i + n
        i = i + 1
    return s

for str in TESTED:
    print(cleanstr(str))

Regular expression?

>>> for s in "23apple", "100", "6Hello", "0 red 37 blue", "01234560":
        t = re.sub('|'.join(f'{i+1}.{{{i}}}' for i in range(9)), '', s)
        print([s, t])

['23apple', 'apple']
['100', '00']
['6Hello', '']
['0 red 37 blue', '0 red blue']
['01234560', '0']

The expression built is 1.{0}|2.{1}|3.{2}|4.{3}|5.{4}|6.{5}|7.{6}|8.{7}|9.{8} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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