how to count the total number of items for the lists contain a specific item in python -
i want count total items lists contain 'a', should 4+3 = 7
tweetword = (['a','b','c','a'],['a','e','f'],['d','g']) count_total_a = {} t in tweetword: 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 not correct coz counts first list twice'''
really appreciate help!
take sum on generator:
sum(len(x) x in tweetword if 'a' in x)
Comments
Post a Comment