我做了这样的事情:

sql_query1=...
sql_query2=...

cur.execute(sql_query1,(actorId1,actorId2))
results1=cur.fetchall()

cur.execute(sql_query2,(actorId1,actorId2))
results2=cur.fetchall()

nested_tuple_list1=[]
for result in results1:
    nested_tuple_list1.append(result)

return(nested_tuple_list1)

nested_tuple_list2=[]
for result in results2:
    nested_tuple_list2.append(result)

return(nested_tuple_list2)


我想得到两个查询的共同结果。关于我该怎么办?

我需要的一个例子:

jim | tim | a      jim | dan | b
dan | mo  | b      dan | mo  | b
jim | dan | c


我想得到:

dan | mo | b

最佳答案

尚不清楚要实现的目标,但是如果要使用列表,最简单的方法是:

intersection=[value for value in nested_tuple_list1 if value in nested_tuple_list2]

上面的内容也适用于列表中的元组,假设您匹配的是整个元组,而不是其中的元素。所以举个例子

a=[(1, 2), (18, 3), (9, 8), (11, 83)]
b=[(4, 3), (8, 47), (42, 77), (1, 2), (3, 18)]

intersection=[value for value in a if value in b]


我得到的结果是:

[(1, 2)]


如果要按列表元素比较列表中的元组,这会稍微复杂一些,但也是可行的。

10-06 05:03
查看更多