I've a nested list in the format:
[[a1, b1, c1, d1], [a2, b2, c2, d2], [a3, b3, c3, d3]]
For each sub-list, I'd like to count the number of times a
(a1, a2, a3, ...)
appears in any of the sub-lists (since a
can potentially appear in multiple sub-lists), and then insert it back into the current sub-list that I'm iterating through.
I've read the threads here and here, and have attempted to do this like so:
for sl in list_in: # Iterate through each sub-list in the big list
c_umi = sl[0] # "c_umi" is "a" in my example above; i.e. extract each "a" value from the big list
u_count = list_in.count(any(c_umi in s_list for s_list in list_in)) # Count the number of times "c_umi" appears in any of the sub-lists in the entire big list, not just the sub-list I'm iterating over at the moment
sl.insert(2, u_count) # Insert the count into index #2 of the current sub-list
I'm not getting any python errors, but the count keeps returning 0. So the value "0" keeps getting inserted into my sub-list, when I can plainly see that there is in fact a
that exists at least once in the sub-list!
Here is a screenshot:
I suspect my list comprehension is incorrect. Does anyone know why this isn't counting correctly?