How to match substring in a string using Python [duplicate]

Multi tool use


How to match substring in a string using Python [duplicate]
This question already has an answer here:
I have a list of substrings that I have to match in a string iteratively; if matching then perform the desired functionality.
The problem is that, whenever I tried to access the list with loops it does not work. Otherwise if I hard code it then it works. I do not understand why it is so?
My code is here:
players_list = ['Circket', 'PSL', 'IPL', 't20', 'shahid afridi', 'aamer yamin']
length = len(players_list)
cur.execute("SELECT tweet FROM tweets_data") # Query for getting specific attribute
length = len(players_list)
for row in cur.fetchall():
i = 0
while (i<length):
#print players_list[i], 'tweet value', row
if players_list[i] in row:
print 'list item:', players_list[i]
print row
else:
print 'Else statement.'
i+=1
Output: it should display the rows only that match with the any of the substring value like:
substring is: cricket
row: Security officials concerned about cricket teams being named after militants
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Players_list[i] in row only matches an exact, complete, same item in row. Try printing row, what Is in it?
– barny
Jun 12 '16 at 12:32
Might as well post Traceback message.
– Iron Fist
Jun 12 '16 at 12:32
@IronFist The code looks fine (even if somewhat unpythonic). I think that this is a case of OP expecting one thing but getting another.
– John Coleman
Jun 12 '16 at 12:35
'Circket'
does not match 'cricket'
– PM 2Ring
Jun 12 '16 at 12:51
'Circket'
'cricket'
2 Answers
2
If I understood your question correctly, you can try this:
players_list = ['Cricket', 'PSL', 'IPL', 't20', 'shahid afridi', 'aamer yamin']
cur.execute("SELECT tweet FROM tweets_data")
for row in cur.fetchall():
if any(True for p in players_list if p.lower() in row.lower()):
print(row)
else:
print ("Else statement")
Thank you.you solved my problem. One thing i have to use the player_list value on each iteration like i have to print player name as well with row. e.g, im doing it like: print(p,':',row) but Error is : p is not defined. any suggestion please.?
– SmartF
Jun 12 '16 at 15:45
You seem to have a typo with Circket
. Also, there is no need to use indices -- just loop over players_list
directly, making sure that both the player and the row have a predictable case. Something like:
Circket
players_list
players_list=['Cricket','PSL','IPL','t20','shahid afridi','aamer yamin']
cur.execute("SELECT tweet FROM tweets_data") # Query for getting specific attribute
for row in cur.fetchall():
for player in players_list:
if player.lower() in row.lower():
print 'list item:', player
print row
else:
print 'Else statement.'
It'd be more efficient to convert the strings in
players_list
to lower case before the loops.– PM 2Ring
Jun 12 '16 at 12:53
players_list
You can
break
out of the inner loop after getting a match, as the question says at least one match is enough. Otherwise same row
will get printed for every word it matches.– Rahul
Jun 12 '16 at 12:54
break
row
@PM2Ring That is doubtless correct in some ways, though there is a chance that the original cases might be important for some purposes. A lower-cased copy of the list can be created, and perhaps converted to a set so that a set intersection (against
set(row.lower().split())
) could replace the inner loop. My guess is that the problem size is too small for these sorts of things to matter much.– John Coleman
Jun 12 '16 at 12:58
set(row.lower().split())
Fair point. And at this stage, the OP is probably best served by the simplest code that does the job.
– PM 2Ring
Jun 12 '16 at 13:02
@Rahul Good point, although the question itself seems somewhat vague about exatly what the intended output should be.
– John Coleman
Jun 12 '16 at 13:04
"it does not work" is not very informative. Please describe the actual problem in more detail. What is the expected output versus the actual output?
– John Coleman
Jun 12 '16 at 12:32