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

9
  • 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? Commented Jun 12, 2016 at 12:32
  • Players_list[i] in row only matches an exact, complete, same item in row. Try printing row, what Is in it? Commented Jun 12, 2016 at 12:32
  • Might as well post Traceback message. Commented Jun 12, 2016 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. Commented Jun 12, 2016 at 12:35
  • 1
    'Circket' does not match 'cricket' Commented Jun 12, 2016 at 12:51

2 Answers 2

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.'
Sign up to request clarification or add additional context in comments.

5 Comments

It'd be more efficient to convert the strings in players_list to lower case before the loops.
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.
@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.
Fair point. And at this stage, the OP is probably best served by the simplest code that does the job.
@Rahul Good point, although the question itself seems somewhat vague about exatly what the intended output should be.
-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")

1 Comment

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.?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.