如何通过列表推导式 python 计算嵌套列表中字符串的 0
How to count the 0`s of a string inside a neste list with python through a list comprehension
我想 return 嵌套列表中零的最大数量,例如下面的列表
lista = [['0000', '', ''], ['', '', '000', ''], ['000', '0', ''], ['', '', '00', '', '']]
对于该列表,它应该 return [4, 3, 3, 2]
我有以下代码给出了错误答案:[0, 0, 1, 0]
print([n.count("0") for n in filas])
我希望使用列表理解来回答它
你快到了。尝试:
print([max(n.count("0") for n in sublist) for sublist in lista])
# Output: [4, 3, 3, 2]
我想 return 嵌套列表中零的最大数量,例如下面的列表
lista = [['0000', '', ''], ['', '', '000', ''], ['000', '0', ''], ['', '', '00', '', '']]
对于该列表,它应该 return [4, 3, 3, 2]
我有以下代码给出了错误答案:[0, 0, 1, 0]
print([n.count("0") for n in filas])
我希望使用列表理解来回答它
你快到了。尝试:
print([max(n.count("0") for n in sublist) for sublist in lista])
# Output: [4, 3, 3, 2]