【发布时间】:2021-09-13 04:00:09
【问题描述】:
基本上是标题所说的,我试图删除所有在字符串中包含数字的名称,我对代码的期望是:
['hello', 'number']
我得到的是:
['hello', 'number', 'Z23', 'Z46', 'U-557', 'U-81']
这里是我正在使用的代码:
allnames1 = ['hello','Z18', 'number','Z20', 'Z23', 'Z28', 'Z46','U-101', 'U-557', 'U-73', 'U-81', 'U-96']
for name in allnames1:
x = re.findall("\d",name)
if len(x) != 0:
allnames1.remove(name)
print(allnames1)
为什么我的代码没有按预期工作?
我怎样才能修改它的代码以获得我的预期输出?
【问题讨论】: