简体   繁体   English

Python:迭代并比较2个元组元素列表

[英]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. 我有2个包含一些元组的列表。

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. 我想比较list_1list_2元素中每个句子的每个元组,并仅返回具有不同标签的元组。

 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 return过早退出循环

The loop is only run once because you return the value on the first iteration. 循环只运行一次,因为您在第一次迭代时return值。 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')]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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