简体   繁体   中英

Python: Iterate through and compare 2 lists of tuples element-wise

I've been struggling for hours and can't work it out, so please help me! I have 2 lists of lists that contain some tuples.

list_1 = [[('This', 'DT'), ('is', 'VBZ'), ('an', 'DT'), ('apple', 'NN')], [('This', 'DT'), ('Should', 'MD'), ('be', 'VB'), ('taught', 'VBN'), ('at','IN'), ('school', 'NN')], [('you', 'PRP'), ('might', 'MD'), ('take', 'VB'), ('them', 'PRP')]]

list_2 = [[('This', 'DT'), ('is', 'VBN'), ('an', 'DT'), ('apple', 'NNS')], [('This', 'DT'), ('Should', 'VB'), ('be', 'VB'), ('taught', 'VBN'), ('at','IN'), ('school', 'NNP')], [('you', 'PRP'), ('might', 'MD'), ('take', 'VB'), ('them', 'PRP')]]

I want to compare each tuple of each sentence in list_1 and list_2 element-wise, and return only the tuples that have different tags.

 def get_similarity(): for list_1_sentence, list_2_sentence in zip(list_1, list_2): if len(list_1) != len(list_2): try: raise Exception('this is an exception, some tags are missing') except Exception as error: print('caught this error : ', repr(error)) else: return [(x, y) for x, y in zip(list_1_sentence, list_2_sentence) if x != y] get_similarity() 

The problem is that it returns the different tuples but for the first sentence only:

[(('is', 'VBZ), ('is', 'VBN)), ('apple', 'NN'), ('apple', 'NNS'))]

Why doesn't iterate through all the sentences?

return exits the loop prematurely

The loop is only run once because you return the value on the first iteration. Instead, track all your results and return at the end of the function, like so:

def get_similarity():
    results = []
    for list_1_sentence, list_2_sentence in zip(list_1, list_2):
        # ...
        results.append([(x, y) for x, y in zip(list_1_sentence, list_2_sentence) if x != y])
    return results

You should see that this works. Try it Online!

You return from the first iteration of your loop. But you can easily turn it into a generator by yielding instead of returning:

def get_similarity():
    for list_1_sentence, list_2_sentence in zip(list_1, list_2):
        #...
        yield [(x, y) for x, y in zip(list_1_sentence, list_2_sentence) if x != y]

list(get_similarity())

You can try this:

list_1 = [[('This', 'DT'), ('is', 'VBZ'), ('an', 'DT'), ('apple', 'NN')], [('This', 'DT'), ('Should', 'MD'), ('be', 'VB'), ('taught', 'VBN'), ('at','IN'), ('school', 'NN')], [('you', 'PRP'), ('might', 'MD'), ('take', 'VB'), ('them', 'PRP')]]

list_2 = [[('This', 'DT'), ('is', 'VBN'), ('an', 'DT'), ('apple', 'NNS')], [('This', 'DT'), ('Should', 'VB'), ('be', 'VB'), ('taught', 'VBN'), ('at','IN'), ('school', 'NNP')], [('you', 'PRP'), ('might', 'MD'), ('take', 'VB'), ('them', 'PRP')]]
final_tags = list(filter(lambda x:x, [[c for c, d in zip(a, b) if c[-1] != d[-1]] for a, b in zip(list_1, list_2)]))

Output:

[[('is', 'VBZ'), ('apple', 'NN')], [('Should', 'MD'), ('school', 'NN')]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM