Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从列表中的列表中删除包含非数值的单元格_Python_List - Fatal编程技术网

Python 如何从列表中的列表中删除包含非数值的单元格

Python 如何从列表中的列表中删除包含非数值的单元格,python,list,Python,List,我有一个列表,其中包含包含数字和文本的子列表。 我想完全删除文本 所以清单是这样的: sample_list = [['hello','there'],['1','2.0','3'],['564','text','65.976']] 我一直在尝试的是 list_without_text = [[item for item in sublist] for sublist in sample_list if item.isdigit()==True ] 但是我得到了NameError:没有定义名

我有一个列表,其中包含包含数字和文本的子列表。 我想完全删除文本

所以清单是这样的:

sample_list = [['hello','there'],['1','2.0','3'],['564','text','65.976']]
我一直在尝试的是

list_without_text = [[item for item in sublist] for sublist in sample_list if item.isdigit()==True ]
但是我得到了NameError:没有定义名称“item”

有什么想法吗


编辑:列表中的所有项目都是字符串,有些项目仍然带有数字、字母和/或小数点。

您弄错了,
item.isdigit()
if条件应该在子列表中。范例-

list_without_text = [[item for item in sublist  if item.isdigit()==True] for sublist in sample_list]
但这只适用于所有元素都是字符串,并且您希望删除非整数的字符串/浮点。这不包括
2.0
等数字

为此,您可以使用如下函数-

def isnum(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

list_without_text = [[item for item in sublist  if isnum(item) ] for sublist in sample_list]

示例/演示-

>>> l = [["Hello","1"],["2.0","Bye"]]
>>> def isnum(s):
...     try:
...         float(s)
...         return True
...     except ValueError:
...         return False
...
>>> list_without_text = [[item for item in sublist  if isnum(item) ] for sublist in l]
>>> list_without_text
[['1'], ['2.0']]
代码:

lst= [["hello","there",],[1,2.0,3],[564,"text",65.976]]

[[ b for b in i if not isinstance(b, basestring)]for i in lst]
[[], [1, 2.0, 3], [564, 65.976]]
lst= [["hello","there",],["1","2.0","3"],["564","text","65.976"]]

[[ b for b in i if not b.isalpha()]for i in lst]
[[], ['1', '2.0', '3'], ['564', '65.976']]
输出:

lst= [["hello","there",],[1,2.0,3],[564,"text",65.976]]

[[ b for b in i if not isinstance(b, basestring)]for i in lst]
[[], [1, 2.0, 3], [564, 65.976]]
lst= [["hello","there",],["1","2.0","3"],["564","text","65.976"]]

[[ b for b in i if not b.isalpha()]for i in lst]
[[], ['1', '2.0', '3'], ['564', '65.976']]
如果您的列表中满是
字符串

代码:

lst= [["hello","there",],[1,2.0,3],[564,"text",65.976]]

[[ b for b in i if not isinstance(b, basestring)]for i in lst]
[[], [1, 2.0, 3], [564, 65.976]]
lst= [["hello","there",],["1","2.0","3"],["564","text","65.976"]]

[[ b for b in i if not b.isalpha()]for i in lst]
[[], ['1', '2.0', '3'], ['564', '65.976']]
输出:

lst= [["hello","there",],[1,2.0,3],[564,"text",65.976]]

[[ b for b in i if not isinstance(b, basestring)]for i in lst]
[[], [1, 2.0, 3], [564, 65.976]]
lst= [["hello","there",],["1","2.0","3"],["564","text","65.976"]]

[[ b for b in i if not b.isalpha()]for i in lst]
[[], ['1', '2.0', '3'], ['564', '65.976']]

它们都是字符串吗,只是有些可能是只包含数字的字符串。将列表列为变量名?对不起,列表不是实际的变量名,举个例子,我会更改。是的,它们都是StringsHanks,我在问题中忘记了逗号,列表中的所有项目实际上都是stringsGreat,非常感谢。我会投票给你,但我是新来的,没有代表:|没关系,我想建议你接受答案,点击答案左侧的勾号,这对社区很有帮助。不过列表中的所有元素都是字符串,谢谢