如何在python中从列表中删除所有整数值
我只是python的初学者,我想知道是否可以从列表中删除所有整数值?例如,文档如下所示如何在python中从列表中删除所有整数值,python,string,Python,String,我只是python的初学者,我想知道是否可以从列表中删除所有整数值?例如,文档如下所示 ['1','introduction','to','molecular','8','the','learning','module','5'] 删除后,我希望文档看起来像: ['introduction','to','molecular','the','learning','module'] 要删除所有整数,请执行以下操作: no_integers = [x for x in mylist if not
['1','introduction','to','molecular','8','the','learning','module','5']
删除后,我希望文档看起来像:
['introduction','to','molecular','the','learning','module']
要删除所有整数,请执行以下操作:
no_integers = [x for x in mylist if not isinstance(x, int)]
但是,示例列表实际上并不包含整数。它只包含字符串,其中一些仅由数字组成。要过滤掉这些内容,请执行以下操作:
no_integers = [x for x in mylist if not (x.isdigit()
or x[0] == '-' and x[1:].isdigit())]
或者:
is_integer = lambda s: s.isdigit() or (s[0] == '-' and s[1:].isdigit())
no_integers = filter(is_integer, mylist)
列表中的项都不是整数。它们是只包含数字的字符串。因此,您可以使用
isdigit
string方法过滤掉这些项
items = ['1','introduction','to','molecular','8','the','learning','module','5']
new_items = [item for item in items if not item.isdigit()]
print new_items
链接到文档:您也可以这样做:
def int_filter( someList ):
for v in someList:
try:
int(v)
continue # Skip these
except ValueError:
yield v # Keep these
list( int_filter( items ))
为什么??因为
int
比试图编写规则或正则表达式来识别编码整数的字符串值要好 请不要使用这种方式从列表中删除项目:(在THC4k发表评论后编辑)
这将不起作用,因为在遍历列表时更改列表将混淆for循环。
此外,如razpeitia所述,如果项是包含负整数的字符串,则item.isdigit()将不起作用。您还可以使用lambdas(显然还有递归)来实现这一点(需要Python 3):
结果(从“l”显示)将是:['hello','world']我个人喜欢过滤器。我认为,如果以明智的方式使用,它可以帮助保持代码可读性和概念上的简单性:
x = ['1','introduction','to','molecular','8','the','learning','module','5']
x = filter(lambda i: not str.isdigit(i), x)
或
注意第二个返回一个迭代器。您可以使用内置的
过滤器来获取列表的过滤副本
>>> the_list = ['1','introduction','to','molecular',-8,'the','learning','module',5L]
>>> the_list = filter(lambda s: not str(s).lstrip('-').isdigit(), the_list)
>>> the_list
['introduction', 'to', 'molecular', 'the', 'learning', 'module']
上面的代码可以通过使用显式类型转换来处理各种对象。由于几乎每个Python对象都可以合法地转换为字符串,因此这里filter
为_列表的每个成员获取一个str转换副本,并检查字符串(减去任何前导'-'字符)是否为数字。如果是,则从返回的副本中排除该成员
它们每一个都针对其设计用于处理的任务进行了高度优化,它们将使您免于重新发明解决方案 从列表中删除所有整数
ls = ['1','introduction','to','molecular','8','the','learning','module','5']
ls_alpha = [i for i in ls if not i.isdigit()]
print(ls_alpha)
如果您想就地修改列表而不是创建列表,这可以通过简单的循环来完成。@mykhal正在检查,这是个玩笑吗?当然,它可以处理多位数的数字course@razpetia:固定处理负数的泛化使这一点不那么清楚。试试S.洛特更具Python风格的答案。@Daniel Stutzbach:你的过滤功能很棒,但你应该使用filter()。保持pythonic,你知道。int
为什么比str.isdigit
好?正如其他人指出的,'-2'。isdigit()
将返回False
+1。有关讨论,请参见(使用float
,但仍然与之相关)。Python确实需要一种机制来防止人们这样做。它看起来会工作,但它不会——尝试使用li=['iterating+removing->skipping',4',5',see?]
(删除4将跳过5,因此它仍保留在列表中)是的,但是更改代码以实现该行为是很简单的。完成后,现在它处理负数。
from itertools import ifilterfalse
x = ifilterfalse(str.isdigit, x)
>>> the_list = ['1','introduction','to','molecular',-8,'the','learning','module',5L]
>>> the_list = filter(lambda s: not str(s).lstrip('-').isdigit(), the_list)
>>> the_list
['introduction', 'to', 'molecular', 'the', 'learning', 'module']
ls = ['1','introduction','to','molecular','8','the','learning','module','5']
ls_alpha = [i for i in ls if not i.isdigit()]
print(ls_alpha)