Python not finding common element in lists
I'm building a program in Python 3 that needs to go through two lists and count how many times the elements of the first list appear in the second. However, even if I plug in two lists that are hard-coded to have common elements, Python says the list doesn't have any common elements.
Here's a minimal, runnable version of my program:
strings = ["I sell","seashells","by the","seashore"] ngramSet = ["by the"] for ngram in ngramSet: print("Ngram: \"" + str(ngram) + "\"") # Should return "by the" twice where it appears twice. occurrences = [element for element in strings if element is ngram] print("Occurrences: " + str(occurrences)) count = len(occurrences) print("Number of times N-gram appears in string" + str(count))
Ngram: "by the" Occurrences:  Number of times N-gram appears in string0
collections.Counterwas made for this!
import collections strings = ["I sell","seashells","by the","seashore"] ngramSet = ["by the"] strings_counter = collections.Counter(strings) for string in ngramSet: print(string, strings_counter[string])
Your approach is correct. The only problem is within your
lambda, where you compare the two strings using
is. You should be comparing them using
==because you are doing equality comparison.
You can make it very short and clear, there are multiple ways but here is one:
strings = ["I sell","seashells","by the","seashore"] ngramSet = ["by the"] # Built in count function for x in ngramSet: print (x, " -> ", strings.count(x)) # Or make it a one-liner print ([(arg, strings.count(arg)) for arg in ngramSet])
Or you could simply use your code because it seems to be working for me.
If you just want to get common elements, then try sets: