小编Age*_*t π的帖子

如何使用filter()从包含字符串和数字的列表中删除所有字符串?

我有一个列表例如

list = [1,2,3,'t',4,5,'fgt',6,7,'string']
Run Code Online (Sandbox Code Playgroud)

我想使用该filter()函数删除所有字符串只留下数字.我可以用常规方法做到这一点,但我不能用过滤方法做任何提示?

所以:

list(filter(type(i)==str,a)))
Run Code Online (Sandbox Code Playgroud)

不会工作...我试图使用它,但仍然不起作用:

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    list(filter(type(a[-1])==str,a))
TypeError: 'bool' object is not callable
Run Code Online (Sandbox Code Playgroud)

python list filter

2
推荐指数
1
解决办法
157
查看次数

标签 统计

filter ×1

list ×1

python ×1