Twitter API - Keyword search with nested list comprehension

63 views Asked by At

I would like to search the text of tweets. With one keyword it works well. Several are a bit annoying: the code gets long when I use or:

userTweets = [tweet for tweet in userTweets if 'OECD' in tweet['test'] or 'Abstimmung' in tweet['test']] I could just go on and add as much or as I have additional keywords.

Instead I tried with a nested list comprehension to shorten the code

keywords = ['Abstimmung', 'Steuern','OECD'] userTweets = [[x for x in keywords if x in tweet] for tweet in userTweets]

Unfortunately it doesn't work. I get this error:

Traceback (most recent call last): File "C:\Users\nmr\Dropbox\Backup\Pultordner 2022\Jobs\Ideas\voxhelvetia\Twitter\15März2023_list keywords.py", line 67, in <module> userTweets.sort(key=lambda x:x['public']['like_count'], reverse = True) File "C:\Users\nmr\Dropbox\Backup\Pultordner 2022\Jobs\Ideas\voxhelvetia\Twitter\15März2023_list keywords.py", line 67, in <lambda> userTweets.sort(key=lambda x:x['public']['like_count'], reverse = True) TypeError: list indices must be integers or slices, not str

Somebody can help?

I tried list comprehension instead of boolean or.

0

There are 0 answers