I have a list of lists of tuples:
[[("AA","AA"),("QQ","")],[("CC",""),("QQ","")],...]
I am trying to count the number of empty values ""
in the second value of the tuples. In the case above it would be:
I needed the answer in a list with the sum of occurrences for each nested list (in the example above it would be [1,2,...].
I was trying something like that
with open(file, 'r') as f:
obj = pickle.load(f)
count=Counter(elem for elem in el for el in obj if elem[0]=="")
Is it possible to somehow use 2 "for" for nested lists?
Use a list comprehension with sum()
and a generator expression:
[sum(1 for t in tups if t[1] == '') for tups in obj]
Demo:
>>> obj = [[("AA", "AA"), ("QQ", "")], [("CC", ""), ("QQ", "")]]
>>> [sum(1 for t in tups if t[1] == '') for tups in obj]
[1, 2]
[sum(0 if x[1] else 1 for x in sub_lst) for sub_lst in big_list]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.