我有一个单词列表,我想做if语句,下面是我的列表:
list = ['camera','display','price','memory'(will have 200+ words in the list)]
这是我的代码:
def check_it(sentences):
if 'camera' in sentences and 'display' in sentences and 'price' in sentences:
return "Camera/Display/Price"
if 'camera' in sentences and 'display' in sentences:
return "Camera/Display"
...
return "Others"
h.loc[:, 'Category'] = h.Mention.apply(check_it)
这些组合将会太多,我也想让单词单独返回行。 有谁知道如何制作此示例并单独返回单词,而不是执行“相机/显示器/价格”操作?
答案 0 :(得分:5)
通过正则表达式使用str.findall
-将列表的所有值与|
联接,最后str.join
个值由/
联接:
df = pd.DataFrame({'Mention':['camera in sentences and display in sentences',
'camera in sentences price']})
L = ['camera','display','price','memory']
pat = '|'.join(r"\b{}\b".format(x) for x in L)
df['Category'] = df['Mention'].str.findall(pat).str.join('/')
print (df)
Mention Category
0 camera in sentences and display in sentences camera/display
1 camera in sentences price camera/price
具有列表理解的另一种解决方案,也适用于join
的列表生成器:
df['Category1'] = [[y for y in x.split() if y in L] for x in df['Mention']]
df['Category2'] = ['/'.join(y for y in x.split() if y in L) for x in df['Mention']]
print (df)
Mention Category1 \
0 camera in sentences and display in sentences [camera, display]
1 camera in sentences price [camera, price]
Category2
0 camera/display
1 camera/price
答案 1 :(得分:1)
some_words = ['camera','display','price','memory']
def check_it(sentences, words):
find_words = []
for word in words:
if word in sentences:
find_words.append(word)
return find_words
t = check_it('display has camera and price is', some_words)
print t
答案 2 :(得分:0)
为什么不只检查每个句子中的单词?
wordsList = ['camera','display','price','memory'(will have 200+ words in the list)]
def check_it(sentence, wordsList):
wordString = ''
flag = False
counter = 0
for word in sentence.split():
if word in wordsList:
if counter != 0:
wordString = wordString + '/' + word
else:
wordString = word
flag = True
counter += 1
if flag:
return wordString
elif not flag:
return 'Others'