Home>
I'm trying to determine if a sentence contains the specified word in Python
For example, in the following case, only cool is included and I want to display it.
text = "The T-shirt looks cool."
words = ['T','shirt','cool','l']
In the case of this example, only "cool" is an exact match, but a partially matching word is also returned.
T
shirt
cool
l
Corresponding source code
text = "The T-shirt looks cool."
words = ['T','shirt','cool','l']
for i in range (len (words)):
if words [i] in text:
print (words [i])
#output
T
shirt
cool
l
What I tried
I also tried putting spaces before and after the specified word, but the result did not change.
if'' + words [i] +'' in text:
text = "The T-shirt looks cool."
words = ['T','shirt','cool','l']
for i in range (len (words)):
if'' + words [i] +'' in text:
print (words [i])
#output
T
shirt
cool
l
Supplementary information (FW/tool version, etc.)
python 3.7.4
-
Answer # 1
-
Answer # 2
if words [i] in text:
if words [i] == text:
PleaseCompare strings in Python (exact match, partial match, magnitude relation, etc.) | note.nkmk.me
Related articles
- i have a question about basic python problems
- python - about write loop to csv
- about image output of python
- python 3x - please teach about the exchange of information with qthead
- python - about x-axis adjustment in matplotlib
- problem that 16bit data comes out as 8bit in python
- python - about "" "of" "" select === = "" "
- python, about the fire spread step of forest fire simulation
- python - about hamiltonian neural networks
- about python argument and data definition
- python 3x - about the amount of data for object detection using machine learning
- python 3x - about downloading anaconda
- python - about the optimum angle of rotation matrix
- python - about downloading youtube videos by youtube-dl
- about processing to exclude the character string group specified from list in python
- python - what i don't understand about yolo9000 (v2)
- about batch change of file name using python
- about the python speedtest code
- python 3x - about functions used in python for statement
- python - about image display and position of main ()
Related questions
- python - i want to sort the list of character strings stored in a lexicographic order in order of appearance frequency
- how to save the image ocr result file in python
- python - i want to add data with pandas data flame i want to add one element of a row
- command does not start in python discordbot
- python - pandas dataframe comparison (dfequals) doesn't work is it because the individual element read from the csv file has a d
- python - [django] isn't the "except pagenoeaninteger" block running in viewspy?
- error in python, subprocess
- [python] functions in the class that inherits formatter do not work
- error in python/discord bot
- python - canvas multiple color change
Perhaps what the questioner wants to do is "find an exact match between the words in the string and the words in the pre-specified word list."
String
split
Divide it into words with, and then remove the disturbing letters at both ends of each word.strip
Remove withDisplay any pre-specified word list elements in the split words