计算列表列表中有空白列表的次数 [英] Counting how many times there are blank lists in a list of list
本文介绍了计算列表列表中有空白列表的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表:
l = [['a', []], ['b', []], ['c', []], ['d', ['e']], ['f', []], ['g', ['h']], ['i', ['j']]]
我想计算多少个列表的第一个元素旁边有元素[]. 例如,在此列表中,我们有4个列表,其第一个元素旁边有一个空白列表.
I want to count how many lists have the element [] next to the first element. for example, in this list we have 4 lists that have a blank list next to its first element.
另一个例子可能是:
l2 = [['a', []], ['b', []], ['c', []], ['d', ['e', 'f']], ['g', ['h', 'i']], ['j', ['k', 'l']], ['m', ['n', 'o']]]
在这里,我们有3个列表,这些列表的元素在列表的第一个元素旁边[].
Here we have 3 lists that have elements that are [] next to the first element of a list.
I tried doing something like, but it returns 0
`````````````
def counting(l):
c = sum(x.count("[]")for x in L)
return c
推荐答案
这是一个单行解决方案,可以满足您的需求
Here is a single line solution doing what you want
def counting(l):
return sum(x.count([]) for x in l)
这篇关于计算列表列表中有空白列表的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文