下面的是我的code,但是最后的结果全部都是others,有哪位大神能帮忙看看是因为什么吗?code的思路是如果B列出现camera,c列就写camera,如果b列出现display,c列就写display,以此类...
下面的是我的code, 但是最后的结果全部都是others,有哪位大神能帮忙看看是因为什么吗? code的思路是 如果B列出现camera, c列就写camera, 如果b列出现display,c列就写display,以此类推,然后最好是能加上如果都有的话,就把所有的词就加上
def check(sentences, words):
'''返回包含至少一个关键词的句子列表'''
return [sentence \
for sentence in sentences\
if sum(sentence.count(word)\
for word in words)>0]
h = pd.read_csv('C:/Users/Desktop/1.csv')
sentences = h['Content']
words = ['camera', 'display', 'screen', 'battary']
result = check(sentences, words)
for item in sentences:
for i in item:
if "camera" in i:
h['Cat'] = "camera"
if "display" in i:
h['Cat'] = "display"
if "screen" in i:
h['Cat'] = "screen"
else:
h['Cat'] = "others"
h.to_csv('C:/Users/Desktop/2.csv')
展开