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

时间:2017-10-26 14:32:41

标签: python list tuples

我已经挣扎了几个小时而无法解决,所以请帮助我!我有2个包含一些元组的列表列表。

SELECT * 
FROM foo
INNER JOIN bar ON bar.foo_id = foo.foo_id

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

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

为什么不遍历所有句子?

3 个答案:

答案 0 :(得分:3)

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

你应该看到这个有效。 Try it Online!

答案 1 :(得分:2)

从循环的第一次迭代返回。但是你可以通过屈服而不是返回来轻松地将它变成一个生成器:

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

答案 2 :(得分: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')]]
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')]]