次の python コードは、私が達成したいことを明確にする必要があります。
# Say I have the following list and I want to keep count of 1's while going through each nested list
L = [[1,1,0,0,0,1],[1,1,0,0,0,0],[0,0,0,0,0,1],[1,1,1,1,1,1]]
# So I'd like to get a list containing [3, 5, 6, 12]
# I tried to find a compact way of doing this by mapping this list into a another list like such
T = [L[:i].count(1) for i in range(len(L))]
# >>> [0, 0, 0, 0]
# But this doesn't work so how to count occurances for nested lists?
# Is there a compact way of doing this (maybe with Lambda functions)?
# I'd like to avoid using a function like:
def Nested_Count():
Result = []
count = 0
for i in L:
count += i.count(1)
Result.append(count)
return Result
# >>> [3, 5, 6, 12]
これを行うためのよりコンパクトなコードが可能かどうか教えてください。
ありがとう!