I am working with a function that would search for a substring in a list of lists, as the first three characters of each item in the list. For example, if the substring is 'aaa' and the list of lists is [['aaa111', 'aba123', 'aaa123'], ['aaa302', 'cad222']], I would like the function to return a list of percentages [66, 50], since 'aaa' appears in the first list 2/3 times, and in the second list 1/2 times. So far I have:
def percentage(list_of_lists, substring):
count = 0
percentage = []
for item in list_of_lists:
for i in item:
if substring == i[0:3]:
count += 1
percentage.append(int(count / len(item) * 100))
return percentage
I understand that my code may be excessive, but I’m just getting the gist of Python so I’m not worried.
>>> percentage([['aaa111', 'aba123', 'aaa123'], ['aaa302', 'cad222']], 'aaa')
[66, 150]
How do I make it count list by list in my list_of_lists?
Two things…
countfor each loopI changed the
count->0.0