pythonjythonjes

Comparing a list of lists to a list in python


I'm trying to to compare a list of lists to a simple list in JES This is a sample of the data that im trying to compare

list1 = [(1, 'abc'), (5, 'no'), (5, 'not'), (10, 'which')]
 list2 = ['not', 'which', 'abc']

Basically what i'm doing is comparing a set of words and their frequencies (list1) with a list of distinct words (list2), if list 2 matches list 1 make a new list that contains the same word and the frequency from list1 This is an example of the list 3 output below

list3 = [(5, 'not'), (10, 'which'), (1, 'abc')]

This is using JES which is missing some of the full functionality of python, so id assume i can only answer this with a for loop or such

this is what I've tried so far, also a few other combinations

list3 = []
    for x in keywords:
     for y in frequencyList:
        if x == y[1]:
            list3.append(y) 

Thank you for any help


Solution

  • In python I would do:

    list3 = [x for x in list1 if x[1] in list2]
    

    Not sure about JES though