【发布时间】:2018-09-04 07:13:00
【问题描述】:
我有 3 个列表,其中 1 个为空。我想计算列表中不为空的列表。
例如:
a = ['username', 'username', 'social']
b = []
c = ['username', 'instead', 'added']
将所有列表组合在一起:
combine = [a,b,c]
计算非空列表:
count = sum(x is not '' for x in combine)
当我运行上述代码时,我得到 'count=3' 但我想要 'count=2'
请帮助我如何做到这一点。
【问题讨论】:
-
您为什么认为
[] == ''是一个空列表? -
好的,所以在这种情况下,我不想计算 [] 并计算休息时间。
-
你应该可以做到
sum(map(bool, combine))或sum(bool(x) for x in combine) -
感谢@cᴏʟᴅsᴘᴇᴇᴅ 它对我有用。
-
这是另一个尝试
len(list(filter(None, combine))