4

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SmartF
  • 99
  • 2
  • 12
  • 3
    "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
  • Players_list[i] in row only matches an exact, complete, same item in row. Try printing row, what Is in it? – DisappointedByUnaccountableMod 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
  • @JohnColeman..did u mean like an [X-Y](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) problem?...I asked for traceback because he said with iterating through list in loops didn't work, so I was expecting that he got an error message. – Iron Fist Jun 12 '16 at 12:38
  • @IronFist Just guessing, I think that OP want's a case-insensitive search but is conducting a case-sensitive search. They seem to describe it as a failure to loop, but the syntax of the loop look fine (unless I am missing something) so I don't think that this is the problem. – John Coleman Jun 12 '16 at 12:42
  • @IronFist there is no traceback error/message. Every time it executes the else statement. – SmartF Jun 12 '16 at 12:50
  • 1
    `'Circket'` does **not** match `'cricket'` – PM 2Ring Jun 12 '16 at 12:51
  • @PM2Ring assume its all in lower case. still not working. Its strange that if statement does nit executes even if the row has substring . Output is : Else statement. WATCH: Nathan McCullum takes a stunning diving catch against India at the #WT20 Else statement. WATCH: Nathan McCullum takes a stunning diving catch against India at the #WT20 list item: t20 WATCH: Nathan McCullum takes a stunning diving catch against India at the #WT20 – SmartF Jun 12 '16 at 13:06

2 Answers2

1

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:

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.'
John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • 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
  • 1
    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
  • @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
  • 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
-1

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")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ravi Kumar
  • 1,769
  • 2
  • 21
  • 31
  • 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