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

时间:2015-11-17 00:55:13

标签: python python-2.7 encoding utf-8 string-comparison

我试图对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

他们都是真的。他们显然不应该这样做。只有两个应该是真的。

编辑: 刚认识到这可能是我处理循环的方式。我如何用我想要的结果做到这一点?

2 个答案:

答案 0 :(得分:2)

要简单地按AllRelationships获取单个值,您需要在content中迭代所有内容之前为boolean flag设置初始值。如果你抓住了它中存在的东西,你可以改变content的值并从循环中断开:

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。而是使用一些有点不同但同样富有表现力的东西,例如listlist1等等。

答案 1 :(得分:1)

变量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