python if multiple string return 句子中包含的单词
python if multiple string return the words that contains in the sentences
我有一个单词列表,我想做 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)
这些组合太多了,我也想把 return 单独排成一行。
有谁知道如何单独制作这个样本和 return 这个词而不是 'camera/display/price'?
使用 str.findall
by regex - join all values of lists with |
, last 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
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
为什么不检查每个句子中的单词?
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'
我有一个单词列表,我想做 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)
这些组合太多了,我也想把 return 单独排成一行。 有谁知道如何单独制作这个样本和 return 这个词而不是 'camera/display/price'?
使用 str.findall
by regex - join all values of lists with |
, last 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
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
为什么不检查每个句子中的单词?
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'