Python:与";如果没有,则为“;
Python函数Python:与";如果没有,则为“;,python,python-2.7,if-statement,any,Python,Python 2.7,If Statement,Any,Python函数any()有问题。我想使用any()函数从列表中删除任何包含数字的项。例如: lst=['abc123','qwerty','123','hello'] 我应用以下代码从lst中删除项目:'abc123','123' new\u list=[lst中的项目如果没有(item.isdigit()表示项目中的字符)] 我希望new_list只包含['qwerty','hello']项,但结果是new_list=lst…没有变化。为什么?我还试着使用import\uuuuu built
any()
有问题。我想使用any()
函数从列表中删除任何包含数字的项。例如:
lst=['abc123','qwerty','123','hello']
我应用以下代码从lst
中删除项目:'abc123'
,'123'
new\u list=[lst中的项目如果没有(item.isdigit()表示项目中的字符)]
我希望
new_list
只包含['qwerty','hello']
项,但结果是new_list
=lst
…没有变化。为什么?我还试着使用import\uuuuu builtin\uuuuuu
和\uuuu builtin\uuuuu。任何
都没有运气。我使用Python2.7。item.isdigit()
应该是char.isdigit()
item.isdigit()
应该是char.isdigit()
你需要检查any
中的char
,而不是item
:
new_list = [item for item in lst if not any(char.isdigit() for char in item)]
或者,您可以使用re
regex模块:
new_list = [item for item in lst if not re.search(r'\d', item)]
您需要检查
任何中的字符
,而不是项目
:
new_list = [item for item in lst if not any(char.isdigit() for char in item)]
或者,您可以使用re
regex模块:
new_list = [item for item in lst if not re.search(r'\d', item)]
啊!!让我哑口无言。。。谢谢,我会在系统允许的情况下批准你的回答(我必须等待8分钟)。啊!让我哑口无言。。。谢谢,当系统允许时,我会批准你的回答(我必须等待8分钟)。