Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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 3.x Python-删除空bool<;类别';str'&燃气轮机;列表中的元素_Python 3.x - Fatal编程技术网

Python 3.x Python-删除空bool<;类别';str'&燃气轮机;列表中的元素

Python 3.x Python-删除空bool<;类别';str'&燃气轮机;列表中的元素,python-3.x,Python 3.x,我想删除列表中的空元素: 当我打印他们的字体时 x = ['((args[0] AND args[1]) OR args[2])', 'OR', ' ((not args[1]) AND (not args[3]) AND args[2])', ''] for i in range(0, len(x)): print("i: "+str(i)+str(x[i])+str(type(x[i]))+str(len(x[i]))) 我明白了: >i: 0((arg

我想删除列表中的空元素:

当我打印他们的字体时

x = ['((args[0] AND args[1]) OR args[2])', 'OR', ' ((not args[1]) AND (not args[3]) AND args[2])', '']

for i in range(0, len(x)):
    print("i: "+str(i)+str(x[i])+str(type(x[i]))+str(len(x[i])))
我明白了:

>i: 0((args[0] AND args[1]) OR args[2])<class 'str'>34
>i: 1OR<class 'str'>2
>i: 2 ((not args[1]) AND (not args[3]) AND args[2])<class 'str'>46
>i: 3<class 'str'>0
我得到一个错误:

 line 5, in <module>
    if len(x[i] == 0):
TypeError: object of type 'bool' has no len()
第5行,在
如果len(x[i]==0):
TypeError:类型为“bool”的对象没有len()
为什么我可以打印它们的长度
len()
它可以工作,但是当我尝试比较它时:
如果(len(x[I])
我得到一个错误


我怎样才能正确地删除它们呢?

下面是我关于正确删除空元素的建议

x = [i for i in x if i != '']
print(x)
输出:

['((args[0] AND args[1]) OR args[2])', 'OR', ' ((not args[1]) AND (not args[3]) AND args[2])']

你期望的
x[i]==0的长度是多少?这不是真的吗?如果我这样做:x=''len(x)==0>TrueYes.和
len(x==0)
=
len(False)
=?是的,但当我试图将其过滤为'False'时,我尝试了这样做:对于范围内的i(0,len(x)):如果x[i]!=False:print(“i:+str(i)”“+x[i])好的,我现在明白我的错误了,唐M。范在下面向我解释了。谢谢你的回答,关于你的问题,现在更清楚了,如果len(x[I])==0:
应该是
,而不是
如果len(x[I]==0):
那么这是你的错误。
['((args[0] AND args[1]) OR args[2])', 'OR', ' ((not args[1]) AND (not args[3]) AND args[2])']