Home > Net >  Filtering out elements in a weirdly structured list in Python
Filtering out elements in a weirdly structured list in Python

Time:12-17

I have a weirdly structure list of the format

mylist =[(['softwar', '3', 'instal', 'instruct'], 'read'), (['3', 'read', 'instruct', 'nis'], 'instal'), (['read', 'instal', 'nis', '2004'], 'instruct'), (['instal', 'instruct', '2004', 'nav'], 'nis'), (['instruct', 'nis', 'nav', '2004'], '2004'), (['nis', '2004', '2004', 'prior'], 'nav'), (['2004', 'nav', 'prior', 'latsni'], '2004'), (['nav', '2004', 'latsni', 'still'], 'prior'), (['2004', 'prior', 'still', 'end'], 'latsni'),........]

I want to filter out relevant words (that are in a list relevant_words) from mylist:

e.g 
relevant_words =['read','instal']
#I would want to just return:
[(['softwar', '3', 'instal', 'instruct'], 'read'),(['3', 'read', 'instruct', 'nis'], 'instal')] 

Could anyone point me in the right direction as to how to do this?

CodePudding user response:

You can use a list comprehension:

[x for x, y in mylist if y in relevant_words]

OUTPUT

[['softwar', '3', 'instal', 'instruct'], ['3', 'read', 'instruct', 'nis']]

If you want to include the relevant words:

[(x,y) for x, y in mylist if y in relevant_words]

or

[x for x in mylist if x[1] in relevant_words]

OUTPUT

[(['softwar', '3', 'instal', 'instruct'], 'read'), (['3', 'read', 'instruct', 'nis'], 'instal')]
  • Related