簡體   English   中英

Python:迭代並比較2個元組元素列表

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

我已經掙扎了幾個小時而無法解決,所以請幫助我! 我有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')]]

我想比較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() 

問題是它返回不同的元組,但僅針對第一句:

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

為什么不遍歷所有句子?

return過早退出循環

循環只運行一次,因為您在第一次迭代時return值。 相反,跟蹤所有結果並在函數結束時返回,如下所示:

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

你應該看到這個有效。 在線試試吧!

您從循環的第一次迭代返回。 但是你可以通過屈服而不是返回來輕松地將它變成一個生成器:

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())

你可以試試這個:

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)]))

輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM