What if I want to return the list of occurrence of the values in list1 as compared to list2 as below?
list1 = [1, 2, 3, 4, 5]
list2 = [5, 6, 7, 8, 9]
I expect to get 0 occurrence of 1, 0 occurrence of 2, 0 occurrence of 3, 0 occurrence of 4, and 1 occurrence of 5; to have a new list as below,
new_list = [1, 0, 0, 0, 0]
See below by implementation. What am I doing wrong?
from collections import Counter
list1 = [1, 2, 3, 4, 5]
list2 = [5, 6, 7, 8, 9]
def matchingStrings(list1, list2):
count_all=Counter(list1)
counts= {x: count_all[x] for x in list2 if x in list1 }
list_output=list(counts.values())
print(list_output)
return list_output
# Write your code here
if __name__ == '__main__':
matchingStrings(list1,list2)
Output
Expected output
[1, 0, 0, 0, 0]
