Comparing 2 lists in python
📁 List, Advanced Python Topics, question
Hello, I have two lists
Ans = ['red','blue','green','white'] Word = ['red','white']I need to compare two lists and output the matching elements into a separate list, say, result. This question has already been asked, but I just can't figure out how to output the identical elements.
The simplest way to count the occurrences of all elements is to use the built-in class from the module:
In [4]: from collections import CounterIn [5]: array = ["Bob", "Alex", "Bob", "John"] In [6]: c = Counter(array)In [7]: cOut[7]: Counter({'Bob': 2, 'Alex': 1, 'John': 1})In [8]: c['Bob']Out[ Log in to leave an answer