1

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))

Output:

Ngram: "by the"
Occurrences: []
Number of times N-gram appears in string0
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Montana Burr
  • 269
  • 4
  • 21

4 Answers4

3

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.

mckuok
  • 744
  • 6
  • 11
1

collections.Counter was 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])
Ben
  • 5,952
  • 4
  • 33
  • 44
0

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.

juliusmh
  • 457
  • 3
  • 12
0

If you just want to get common elements, then try sets:

list(set(strings).intersection(set(ngramSet)))
whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44