def lists(A: list) -> int:
'''Return the total number of lists in A (including A itself).
Each element of A and any nested lists are either ints or other lists.
Example:
>>> lists([1, 2, 3])
1
>>> lists([[1], [2], [3]])
4
>>> lists([[[1, 2], [], 3]])
4
'''
有人知道怎么做吗? 我所拥有的只是
for i in range(0, len(A)):
if (isinstance(A[i], list)):
count=count+1
return(lists(A[i]))
else:
B=A[i:]
return(count)
您应该使用递归执行此操作:
这里有一个'肮脏'但很容易做到这一点
不需要递归
有一种方法可以写:
示例输出:
相关问题 更多 >
编程相关推荐