2

I'm trying to go parse some text and looking for certain patterns. One pattern I'm looking for is strings that contain both ' AS ' and '.'.

Assuming I have a list of strings, I'm using this to find and append select strings to a new list.

list1 = []

for ngram in bigrams:
        if ' AS ' and '.' in ngram:
            list1.append(ngram)

This is not working, it appears to be functioning more like:

    if ' AS ' or '.' in ngram:

Any suggestions?

screechOwl
  • 27,310
  • 61
  • 158
  • 267

1 Answers1

2
if ' AS ' and '.' in ngram:

is parsed as

if (' AS ') and ('.' in ngram):

that is,

if (True) and ('.' in ngram):

which is equivalent to just if '.' in ngram.

In fact, you cannot use "foo and bar in baz." Try instead:

if ' AS ' in ngram and '.' in ngram:

Or even better, use that in a list comprehension and change those 5 lines of code into one:

list1 = [ngram for ngram in bigrams if ' AS ' in ngram and '.' in ngram]
tckmn
  • 57,719
  • 27
  • 114
  • 156