samedi 9 novembre 2019

How to create a 3rd list from a nested list based on items in another list

I have a list of some users

list_of_users=['@elonmusk', '@YouTube','@FortniteGame','@BillGates','@JeffBezos']

and a nested list made by tweets splitted by words.

tweets_splitted_by_words=[['@MrBeastYT', '@BillGates', 'YOU’RE', 'THE', 'LAST', 'ONE', 'FINISH', 'THE', 'MISSION', '#TeamTrees'], ['@MrBeastYT', '@realDonaldTrump', 'do', 'something', 'useful', 'with', 'your', 'life', 'and', 'donate', 'to', '#TeamTrees'], ['Please', 'please', 'donate']]

I want to create a third new list made by subblists of tweets_splitted_by_words only if each subblist contains at least one of the users in list_of_users. Output that I want:

output=[['@MrBeastYT', '@BillGates', 'YOU’RE', 'THE', 'LAST', 'ONE', 'FINISH', 'THE', 'MISSION', '#TeamTrees']]

I tried to write the following code but it didn't work out.

tweets_per_user_mentioned= []
giorgia=[]
for r in range(len(tweets_splitted_by_words)):
    giorgia.append(r)
    for _i in range(len(giorgia)):
        if _i  in range(len(list_of_users)):
         tweets_per_user_mentioned.append(tweets_splitted_by_words[r])
        else:
            pass
print(tweets_per_user_mentioned)

Aucun commentaire:

Enregistrer un commentaire