比较 python 中的字符串列表
compare string list in python
我在 python 中写了一段代码,但我不确定为什么它在应该能够匹配的时候却始终无法匹配,您介意检查一下我的代码中有什么问题吗?
我的假设是我的 select 查询创建列表列表 "('bad' , ), ('worse' , ), 但我不确定如何删除它 " ,"
我正在使用 visual studio 和 python 3.5:
if len(list(set(words) & set(badlst))) > 0:
index +=1
words :(data details) ['term', 'saving', 'insurance', 'cost', '?',
'upgrade', 'plan', 'always', 'preferred', 'client', ',', 'bad',
'agent', 'explained', ...]
badlst : (data details) [('bad',), ('worse',), ('incorrigible',),
('poor',), ('dreadful',), ('atrocious',), ('cheap',),
('unacceptable',), ('sad',), ('lousy',), ('crummy',), ('awful',),
('rough',), ('synthetic',), ...]
我生成的 badlst 如下:
def readInsCmp(column, tablename, whereCondition):
command = "select "+ column+" from "+ tablename +" where "+ whereCondition
c.execute(command)
namelist = c.fetchall()
return namelist
badlst = readInsCmp("distinct(word)","wordVals","value=-1")
Words 参数基于解析来自 excel 文件的一些输入:
sentenceArr = sent_tokenize(content)
for sentence in sentenceArr:
words = [word for word in nltk.word_tokenize(sentence) if word not in stopwords.words('english')]
如果我对问题的解释正确,你有两个列表:
>>> words = ['term', 'saving', 'insurance', 'cost', '?', 'upgrade', 'plan', 'always', 'preferred', 'client', ',', 'bad', 'agent', 'explained', ]
>>> badlst = [('bad',), ('worse',), ('incorrigible',), ('poor',), ('dreadful',), ('atrocious',), ('cheap',), ('unacceptable',), ('sad',), ('lousy',), ('crummy',), ('awful',), ('rough',), ('synthetic',), ]
并且,您想查找这两个列表是否有任何共同的词。如果是这样,首先要做的是将 badlst
从元组列表转换为单词列表:
>>> badlst2 = [x[0] for x in badlst]
完成后,很容易找到共同点:
>>> set(words).intersection(badlst2)
{'bad'}
我们可以在 if
语句中使用该交集,如下所示:
>>> index = 0
>>> if set(words).intersection(badlst2):
... index += 1
...
>>> index
1
if
语句有效,因为根据 python 约定,如果集合为空,则其布尔值为 False,否则为 True。
作为 if
语句的替代方法,我们可以将集合的布尔值添加到 index
:
>>> index = 0
>>> index += bool( set(words).intersection(badlst2) )
>>> index
1
放在一边
在 python 中,&
执行 bitwise-and,而不是交集。这不是你想要的。
我在 python 中写了一段代码,但我不确定为什么它在应该能够匹配的时候却始终无法匹配,您介意检查一下我的代码中有什么问题吗?
我的假设是我的 select 查询创建列表列表 "('bad' , ), ('worse' , ), 但我不确定如何删除它 " ,"
我正在使用 visual studio 和 python 3.5:
if len(list(set(words) & set(badlst))) > 0:
index +=1
words :(data details) ['term', 'saving', 'insurance', 'cost', '?', 'upgrade', 'plan', 'always', 'preferred', 'client', ',', 'bad', 'agent', 'explained', ...]
badlst : (data details) [('bad',), ('worse',), ('incorrigible',), ('poor',), ('dreadful',), ('atrocious',), ('cheap',), ('unacceptable',), ('sad',), ('lousy',), ('crummy',), ('awful',), ('rough',), ('synthetic',), ...]
我生成的 badlst 如下:
def readInsCmp(column, tablename, whereCondition):
command = "select "+ column+" from "+ tablename +" where "+ whereCondition
c.execute(command)
namelist = c.fetchall()
return namelist
badlst = readInsCmp("distinct(word)","wordVals","value=-1")
Words 参数基于解析来自 excel 文件的一些输入:
sentenceArr = sent_tokenize(content)
for sentence in sentenceArr:
words = [word for word in nltk.word_tokenize(sentence) if word not in stopwords.words('english')]
如果我对问题的解释正确,你有两个列表:
>>> words = ['term', 'saving', 'insurance', 'cost', '?', 'upgrade', 'plan', 'always', 'preferred', 'client', ',', 'bad', 'agent', 'explained', ]
>>> badlst = [('bad',), ('worse',), ('incorrigible',), ('poor',), ('dreadful',), ('atrocious',), ('cheap',), ('unacceptable',), ('sad',), ('lousy',), ('crummy',), ('awful',), ('rough',), ('synthetic',), ]
并且,您想查找这两个列表是否有任何共同的词。如果是这样,首先要做的是将 badlst
从元组列表转换为单词列表:
>>> badlst2 = [x[0] for x in badlst]
完成后,很容易找到共同点:
>>> set(words).intersection(badlst2)
{'bad'}
我们可以在 if
语句中使用该交集,如下所示:
>>> index = 0
>>> if set(words).intersection(badlst2):
... index += 1
...
>>> index
1
if
语句有效,因为根据 python 约定,如果集合为空,则其布尔值为 False,否则为 True。
作为 if
语句的替代方法,我们可以将集合的布尔值添加到 index
:
>>> index = 0
>>> index += bool( set(words).intersection(badlst2) )
>>> index
1
放在一边
在 python 中,&
执行 bitwise-and,而不是交集。这不是你想要的。