无法成功地将字符串与 python 进行比较

Can't successfully compare strings with python

我正在尝试对来自 craigslist 的帖子进行排序,但编码妨碍了我。

这是代码

# -*- 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

结果都是真的。他们显然不应该。只有两个应该是真的。

编辑: 刚刚意识到这可能是我处理循环的方式。我该怎么做才能得到我想要的结果?

要简单地为每个 content 获取一个值,您需要在迭代 content 中的所有内容之前为 boolean flag 设置一个初始值;如果你抓住了其中存在的东西,你改变 flag 的值并跳出循环:

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)

现在产生:

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 

另外,你不应该为你的 list 使用像 list 这样的名字,因为它掩盖了内置类型 list。而是使用一些不同但同样具有表现力的东西,比如 list1my_list 等等。

变量 outcome 应该在 for 循环之外初始化。这是简化的代码

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