Python 检查字符串和列表的列表是否为空
如何检查此列表是否为空Python 检查字符串和列表的列表是否为空,python,Python,如何检查此列表是否为空 l = ['',['']] 我尝试了如何查找嵌套列表是否为空的解决方案。但没有一个成功 def isListEmpty(inList): if isinstance(inList, list): # Is a list return all( map(isListEmpty, inList) ) return False # Not a list 试试这个 l = [' ',[ ]] def isListEmpty(thisList)
l = ['',['']]
我尝试了如何查找嵌套列表是否为空的解决方案。但没有一个成功
def isListEmpty(inList):
if isinstance(inList, list): # Is a list
return all( map(isListEmpty, inList) )
return False # Not a list
试试这个
l = [' ',[ ]]
def isListEmpty(thisList):
for el in thisList:
if (len(el)==0 and type(el)==list):
print('empty') # Or whatever you want to do if you encounter an empty list
isListEmpty(l)
如果您遇到任何问题,请在下面发表评论
l = [' ',[ ]]
def isListEmpty(thisList):
for el in thisList:
if (len(el)==0 and type(el)==list):
print('empty') # Or whatever you want to do if you encounter an empty list
isListEmpty(l)
如果您在下面对实际为空的列表进行注释时遇到任何问题,则函数只需返回True即可
def isListEmpty(inList):
if isinstance(inList, list): # Is a list
if len(inList) == 0:
return True
else:
return all(map(isListEmpty, inList))
return False # Not a list
对于实际为空的列表,函数只应返回True
def isListEmpty(inList):
if isinstance(inList, list): # Is a list
if len(inList) == 0:
return True
else:
return all(map(isListEmpty, inList))
return False # Not a list
在递归检查列表项之前,应该先检查列表是否为falsy/empty。您还可以使用
和和以及或运算符避免显式返回True
或False
:
def isListEmpty(inList):
return inList == '' or isinstance(inList, list) and (not inList or all(map(isListEmpty, inList)))
演示:在递归检查列表项之前,应该先检查列表是否为假/空。您还可以使用和和以及或运算符避免显式返回True
或False
:
def isListEmpty(inList):
return inList == '' or isinstance(inList, list) and (not inList or all(map(isListEmpty, inList)))
演示:l
实际上不是空的。但在这种情况下,此代码应该可以工作:
l = ['',['']]
def isListEmpty(inList):
for char in inList:
if char == '' or ['']:
return True
else:
return False
break
print(isListEmpty(l))
l
实际上不是空的。但在这种情况下,此代码应该可以工作:
l = ['',['']]
def isListEmpty(inList):
for char in inList:
if char == '' or ['']:
return True
else:
return False
break
print(isListEmpty(l))
您可以对任何使用简单的递归方法。使用any
可以确保递归搜索在找到非空项时立即结束
>>> def is_non_empty_list (l):
... return any(is_non_empty_list(e) if isinstance(e, list) else e for e in l)
...
>>> def is_empty_list (l):
... return not is_non_empty_list(l)
...
>>> is_empty_list(['', ['']])
True
>>> is_empty_list(['', ['a']])
False
>>>
您可以对任何使用简单的递归方法。使用any
可以确保递归搜索在找到非空项时立即结束
>>> def is_non_empty_list (l):
... return any(is_non_empty_list(e) if isinstance(e, list) else e for e in l)
...
>>> def is_empty_list (l):
... return not is_non_empty_list(l)
...
>>> is_empty_list(['', ['']])
True
>>> is_empty_list(['', ['a']])
False
>>>
你必须检查嵌套列表是否为空吗?检查它是否为零?@Austin,该字符串实际上不是空的。@某个程序员,我很确定OP的意思是空字符串,尽管有一个像你说的空格。它是空字符串。我用空格来表示清楚你必须检查嵌套列表是否为空吗?检查它是否为零吗?@Austin该字符串实际上不是空的。@某个程序员,我很确定OP的意思是空字符串,尽管有一个像你说的空格。它是空字符串。我利用这个空间使它变得清晰它适合我。您能提供一个失败的示例列表吗?我添加了一个有问题的列表,该列表包含一个字符串,您的示例代码显式地为任何非列表的数据类型返回非空。如果这不是你想要的行为,你应该说清楚。是的,这是一个列表(空字符串和空字符串列表),它对我有效。您能提供一个失败的示例列表吗?我添加了一个有问题的列表,该列表包含一个字符串,您的示例代码显式地为任何非列表的数据类型返回非空。如果这不是你想要的行为,你可以说清楚。是的,它是一个列表(空字符串和空字符串列表),对于所有事情它都返回false,对于[[]]它返回True
虽然不是我要检查的列表,所以你考虑一个空字符串空的列表。在这种情况下,我相应地更新了我的答案。请检查它。返回它的所有错误,<代码> > <代码> >代码> [[[]]/>代码>示例。真的,但这不是我要检查的列表,所以你考虑一个空字符串空的列表吗?在这种情况下,我已经更新了我的答案。请查看。对于像['',['',['',['']]]
这样的更深层次的列表,您觉得如何?这就是为什么其他人采用递归方法的原因,因为它可以是无限深的。对于像['',['',['',[']]]]
这样的更深层次的列表呢?这就是为什么其他人采用递归方法,因为它可以无限深