FallenSpaces
FallenSpaces

Reputation: 352

Can't successfully compare strings with python

I'm trying to sort posts from craigslist, but encoding is getting in my way.

Here's the code

# -*- coding: utf-8 -*-

sort_list = ['position title:   construction laborer  position summary:   assisting on construction site with cleanup and other general labor. previous construction experience is   preferred. safety conscious is a must!  essential duties and responsibilities  ● general site cleanup','i\'m moving a small church organ about 3 miles from west linn to oregon city monday morning. i need to a service to move it. it is being moved from the first level in one house to a first level in another house. it\'s about the size of but definitely lighter than a smaller upright piano. i would need it movedand monday morning at about 9 a.m. please email me your quote thank you ','position title:  cdl a driver  position summary:  local moving company looking for a class a cdl driver to operate a 53 ft tractor‐trailer. only candidates willing to   assist with local and commercial moves will be considered. this is a local driving job, drivers are home every   night.  essential duties and responsibilities:  ● safely navigate moving trucks between city and residential neighborhoods.  ● steady year round work. ']


## List of stuff we don't want
list = ['CDL','Full Time','Part Time','Drivers','Salary','Background Check','Resume','valid drivers license','social security','career','Full-Time','part-time']

for content in sort_list:
    content = content.lower()
    for thing in list:
        thing = thing.lower()
        if thing in content:
            outcome = False
        elif thing not in content:
            outcome = True

    print "\n\n",outcome,"\n\n",content

They all come out as True. They obviously shouldn't. Only two should be True.

Edit: Just had a realization that it may be the way I'm handling the loop. How do I do this with the outcome that I want?

Upvotes: 0

Views: 68

Answers (2)

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160407

To simply get a single value per content you need to set an initial value for a boolean flag before iterating for everything in content; if you catch a thing that exists in it, you change the value of flag and break from the loop:

for content in sort_list:
    flag = True
    content = content.lower()
    for thing in list1:
        thing = thing.lower()
        if thing in content:
            flag = False
            break
    if flag:
        print ("\n\n",outcome,"\n\n",content)

Which now yields:

True 

position title:   construction laborer  position summary:   assisting on construction site with cleanup and other general labor. previous construction experience is   preferred. safety conscious is a must!  essential duties and responsibilities  ● general site cleanup


True 

i'm moving a small church organ about 3 miles from west linn to oregon city monday morning. i need to a service to move it. it is being moved from the first level in one house to a first level in another house. it's about the size of but definitely lighter than a smaller upright piano. i would need it movedand monday morning at about 9 a.m. please email me your quote thank you 

Additionally, you should not use names like list for your list, because it masks the built in type list. Instead use something a bit different but equally expressive, like list1, my_list and so on.

Upvotes: 2

uthavaakkarai
uthavaakkarai

Reputation: 178

variable outcome should be initialized outside the for loop. Here is the simplified code

for content in sort_list:
    content = content.lower()
    outcome = True
    for thing in list:
        thing = thing.lower()
        if thing in content:
            outcome = False

Upvotes: 1

Related Questions