本文介绍了删除Python列表中包含数字的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从大量字符串中删除所有包含数字的项的最佳方法是什么?
What is an optimal way of removing all items containing a number from a large list of strings?
输入:['This','That','Those4423','42','13b''是','2']
Input: ['This', 'That', 'Those4423', '42', '13b' 'Yes', '2']
输出:['This','That','Yes']
Output: ['This', 'That', 'Yes']
推荐答案
>>> foo = ['This', 'That', 'Those4423', '42', '13b', 'Yes', '2']
>>> foo1 = [x for x in foo if not any(x1.isdigit() for x1 in x)]
>>> foo
['This', 'That', 'Those4423', '42', '13b', 'Yes', '2']
>>> foo1
['This', 'That', 'Yes']
>>>
但是,您可以使用.isalpha()
检查字符串是否仅包含字母字符.
However you can use .isalpha()
to check if the string contains alphabetic characters only.
.isaplha()
[x for x in foo if x.isalpha()]
这篇关于删除Python列表中包含数字的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!