[英]python: check any number of nested list to conclude if empty or not
so basically I'm trying to create a function that will check a list which may or may not contain many nested layers of lists, to see if they are all empty. 所以基本上我想创建一个函数来检查一个列表,该列表可能包含也可能不包含列表的许多嵌套层,以查看它们是否都是空的。
so 所以
empty([])
will return true so will empty([['']])
and so will empty([[[]]])
empty([])
将返回true,因此empty([['']])
和empty([[[]]])
I've tried 我试过了
def empty(seq):
try:
return all(map(empty, seq))
except TypeError:
return False
but running into RuntimeError: maximum recursion depth exceeded
error when I run this. 但遇到
RuntimeError: maximum recursion depth exceeded
我运行此代码时, RuntimeError: maximum recursion depth exceeded
错误。
The problematic case string is this : ["[u'3D Blu-ray Disc\\\™ Player (BD-D7500)']"]
I have no idea why that is causing this problem 有问题的情况字符串是这样的:
["[u'3D Blu-ray Disc\\\™ Player (BD-D7500)']"]
我不知道为什么这会导致此问题
["[u'3D Blu-ray Disc\\u2122 Player (BD-D7500)']"]
maximum recursion depth exceeded in cmp
Traceback (most recent call last):
File "./dashboard.py", line 583, in download
return get_rows_from_db(str(job.id), job.url,taskid, options,trial)
File "./dashboard.py", line 123, in get_rows_from_db
print "B " + empty(col)
File "./dashboard.py", line 52, in empty
for item in seq)
File "./dashboard.py", line 52, in <genexpr>
for item in seq)
File "./dashboard.py", line 52, in empty
for item in seq)
File "./dashboard.py", line 52, in <genexpr>
for item in seq)
File "./dashboard.py", line 52, in empty
for item in seq)
File "./dashboard.py", line 52, in <genexpr>
for item in seq)
File "./dashboard.py", line 52, in empty
for item in seq)
File "./dashboard.py", line 52, in <genexpr>
for item in seq)
File "./dashboard.py", line 52, in empty
for item in seq)
File "./dashboard.py", line 52, in <genexpr>
for item in seq)
File "./dashboard.py", line 52, in empty
for item in seq)
File "./dashboard.py", line 52, in <genexpr>
for item in seq)
File "./dashboard.py", line 52, in empty
for item in seq)
File "./dashboard.py", line 52, in <genexpr>
for item in seq)
File "./dashboard.py", line 52, in empty
for item in seq)
File "./dashboard.py", line 52, in <genexpr>
for item in seq)
File "./dashboard.py", line 52, in empty
for item in seq)
File "./dashboard.py", line 52, in <genexpr>
for item in seq)
File "./dashboard.py", line 52, in empty
for item in seq)
File "./dashboard.py", line 52, in <genexpr>
for item in seq)
File "/usr/lib/python2.7/abc.py", line 132, in __instancecheck__
if subclass is not None and subclass in cls._abc_cache:
File "/usr/lib/python2.7/_weakrefset.py", line 73, in __contains__
return wr in self.data
RuntimeError: maximum recursion depth exceeded in cmp
def empty(L_or_I):
if not L_or_I:
return True
if isinstance(L_or_I,(list,tuple)):
return all(empty(x) for x in L_or_I)
return False
I guess ... maybe 我想...也许
If a list of 0 is an "empty", Joran answer is certainly correct. 如果列表0为“空”,那么Joran的答案肯定是正确的。
If you only want to check iterable, I would suggest: 如果您只想检查可迭代,我建议:
import collections
def empty(e):
if not isinstance(e, collections.Iterable):
return False
if not e:
return True
return all(empty(x) for x in e)
This function satisfies each of the test cases you provide. 此功能满足您提供的每个测试用例。
import collections
def empty(seq):
if isinstance(seq, basestring):
return seq == ''
return all(empty(item)
if isinstance(item, collections.Sequence)
else False
for item in seq)
assert empty([])
assert empty([['']])
assert empty([[[]]])
assert not empty([[[], 1]])
assert empty(u'')
assert not empty(u'1')
assert not empty(["[[u'3D Blu-ray Disc\\u2122 Player (BD-D7500)']"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.