python-删除与字符串中的数字对应的大量字符

python-删除与字符串中的数字对应的大量字符,python,Python,我试图编写一个函数,返回一个类似于已消费字符串s的字符串,但是每次在s中出现一个数字时,都应该从字符串中删除与该数字对应的一些字符,包括数字本身。如果删除一个数字的子字符串会删除另一个数字, 那么第二个数字就不应该考虑了。 例如: cleanstr("23apple") -> "apple" cleanstr("100") -> "00" cleanstr("6Hello") -> "" cleanstr("0 red 37 blue") -> "0 red blue"

我试图编写一个函数,返回一个类似于已消费字符串s的字符串,但是每次在s中出现一个数字时,都应该从字符串中删除与该数字对应的一些字符,包括数字本身。如果删除一个数字的子字符串会删除另一个数字, 那么第二个数字就不应该考虑了。 例如:

cleanstr("23apple") -> "apple"
cleanstr("100") -> "00"
cleanstr("6Hello") -> ""
cleanstr("0 red 37 blue") -> "0 red blue"
当字符串中有连续数字时,我的代码不会返回预期结果。例如,cleanstr(“01234560”)返回“0260”而不是“0”。所以我知道循环中的问题是,在检查“0”和“1”之后,它跳过“2”并移动到“3”。有人能帮我解决这个问题吗

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)
def cleanstr(s):
i=0
lst=列表
而i
在每次迭代中,似乎更容易将其视为追加当前字符或将计数器向前推进
i

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'
def cleanstr(s):
i=0
res=''
而我0:
i+=int(s[i])
其他:
res+=s[i]
i+=1
返回res
cleanstr(“0红色37蓝色”)
#“0红蓝”
cleanstr(“23apple”)
#“苹果”
cleanstr(“01234560”)
# '0'

在每次迭代中,似乎更容易将其视为追加当前字符或将计数器向前推进
i

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'
def cleanstr(s):
i=0
res=''
而我0:
i+=int(s[i])
其他:
res+=s[i]
i+=1
返回res
cleanstr(“0红色37蓝色”)
#“0红蓝”
cleanstr(“23apple”)
#“苹果”
cleanstr(“01234560”)
# '0'

您可以从字符串创建一个迭代器来遍历字符,仅当字符不是1到9之间的数字时才输出字符,或者使用
itertools.islice从迭代器中减去1的给定字符数:

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)),))
从itertools导入islice
def cleanstr(多个):
i=国际热核实验堆

返回“”。如果不是“0”,则连接(c代表i中的c)itertools.islice
从迭代器中使用给定数量的字符减去1:

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)),))
从itertools导入islice
def cleanstr(多个):
i=国际热核实验堆
返回“”。加入(如果不是'0'TESTED=[“23apple”,“100”,“6Hello”,“0 red 37 blue”]
def cleanstr(多个):
i=0
而i=0和n
TESTED=[“23apple”、“100”、“6Hello”、“0 red 37 blue”]
def cleanstr(多个):
i=0
而i=0和n正则表达式

>>> 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']
构建的表达式是
1.{0}2.{1}3.{2}4.{3}5.{4}6.{5}7.{6}8.{7}9.{8}
正则表达式

>>> 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']

构建的表达式是
1.{0}2.{1}3.{2}4.{3}5.{4}6.{5}7.{6}8.{7}9.{8}

我不明白:根据您的描述,
0260
是较长字符串的正确答案。您如何得出结论,它应该只返回
0
?首先它应该删除“1”,然后因为“2”是数字,所以应该删除“23”。然后因为“4”是数字,所以也应该删除“4560”,因此只有“0”是leftedAh…我现在明白了。Duh。我不明白:根据您的描述,
0260
是较长字符串的正确答案。您如何得出结论,它应该只返回
0
?首先它应该删除“1”,然后因为“2”是一个数字,所以“23”应该删除。然后因为“4”是一个数字,所以“4560”也应该删除,然后再删除我们只剩下“0”了…我现在明白了。