如何计算python中包含特定项的列表的项目总数
我想计算包含“a”的列表的项目总数,它应该是4+3=7如何计算python中包含特定项的列表的项目总数,python,Python,我想计算包含“a”的列表的项目总数,它应该是4+3=7 tweetword = (['a','b','c','a'],['a','e','f'],['d','g']) count_total_a = {} for t in tweetword: for word in tweetword: if word not in count_total_a: count_toal_a[word] = len(t) if word in cou
tweetword = (['a','b','c','a'],['a','e','f'],['d','g'])
count_total_a = {}
for t in tweetword:
for word in tweetword:
if word not in count_total_a:
count_toal_a[word] = len(t)
if word in count_total_a:
count_total_a[word] += len(t)
'''the result I get is not correct coz it counts the first list twice'''
非常感谢您的帮助 取发电机上的总和:
tweetword = (['a','b','c','a'],['a','e','f'],['d','g'])
contains_a = [tweet for tweet in tweetword if 'a' in tweet]
sum_lengths = sum(map(len, contains_a))
print(sum_lengths)
sum(len(x) for x in tweetword if 'a' in x)