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.
Corresponding source code
T shirt cool l
What I tried
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
I also tried putting spaces before and after the specified word, but the result did not change.
if'' + words [i] +'' in text:
Supplementary information (FW/tool version, etc.)
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
Answer # 1
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."
splitDivide it into words with, and then remove the disturbing letters at both ends of each word.
Display any pre-specified word list elements in the split words
text = "The T-shirt looks cool." words = ['T','shirt','cool','l'] splitted = list (map (lambda x: x.strip ('.,!?'), Text.split ())) print (splitted) for word in words: if word in splitted: print (word)
Answer # 2
if words [i] in text:
if words [i] == text:
Compare strings in Python (exact match, partial match, magnitude relation, etc.) | note.nkmk.me
- 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 ()
- 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