Python 从给定字符串中删除包含数字的单词

Python 从给定字符串中删除包含数字的单词,python,regex,list,iteration,Python,Regex,List,Iteration,我试图编写一个简单的程序,从接收到的字符串中删除所有包含数字的单词 以下是我当前的实现: import re def checkio(text): text = text.replace(",", " ").replace(".", " ") .replace("!", " ").replace("?", " ").lower() counter = 0 words = text.split() print words for each in wo

我试图编写一个简单的程序,从接收到的字符串中删除所有包含数字的单词

以下是我当前的实现:

import re

def checkio(text):

    text = text.replace(",", " ").replace(".", " ") .replace("!", " ").replace("?", " ").lower()
    counter = 0
    words = text.split()

    print words

    for each in words:
        if bool(re.search(r'\d', each)):
            words.remove(each)

    print words

checkio("1a4 4ad, d89dfsfaj.")
但是,当我执行此程序时,我会得到以下输出:

['1a4', '4ad', 'd89dfsfaj']
['4ad']

我不明白为什么
'4ad'
打印在第二行,因为它包含数字,应该从列表中删除。有什么想法吗?

显然发生的是并发访问错误。即-在遍历数组时删除元素

在第一次迭代中,我们有words=['1a4','4ad','d89dfsfaj']。因为'1a4'有一个数字,所以我们将其删除。
现在,words=['4ad','d89dfsfaj']。但是,在第二次迭代中,当前单词现在是'd89dfsfaj',我们将其删除。我们跳过了“4ad”,因为它现在在索引0处,for循环的当前指针在1处。

如果要测试字母数字字符串,为什么不使用
isalnum()
而不是regex

In [1695]: x = ['1a4', '4ad', 'd89dfsfaj']

In [1696]: [word for word in x if not word.isalnum()]
Out[1696]: []

假设正则表达式满足您的要求,可以这样做以避免在迭代时删除

import re

def checkio(text):

    text = re.sub('[,\.\?\!]', ' ', text).lower()
    words = [w for w in text.split() if not re.search(r'\d', w)]
    print words ## prints [] in this case
另外,请注意,我简化了
text=text.replace(…)

此外,如果不需要重用
文本
变量,可以使用regex直接拆分它

import re

def checkio(text):

    words = [w for w in re.split('[,.?!]', text.lower()) if w and not re.search(r'\d', w)]
    print words ## prints [] in this case

这可以通过使用
re.sub
re.search
list\u comprehension
实现

>>> import re
>>> def checkio(s):
        print([i for i in re.sub(r'[.,!?]', '', s.lower()).split() if not re.search(r'\d', i)])


>>> checkio("1a4 4ad, d89dfsfaj.")
[]
>>> checkio("1a4 ?ad, d89dfsfaj.")
['ad']

在对列表进行迭代时,在修改列表时添加预期输出。看看这个问题,你为什么不应该这样做:你到底想完成什么?你的约束和条件是什么?与其说是约束,不如说是理解为什么会出错。现在我明白了,我正在修改一个我正在迭代的列表,这是有意义的。re.search返回一个re.MatchObject