pymysql-合并两次不同查询的结果
pymysql-Combining the results of two different queried into
我做过这样的事情:
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)
我想获得这两个 queries.Any 想法的共同结果,我可以做什么?
我需要的例子:
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)]
如果你想按元素比较列表中的元组,这有点复杂但也是可行的。
我做过这样的事情:
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)
我想获得这两个 queries.Any 想法的共同结果,我可以做什么?
我需要的例子:
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)]
如果你想按元素比较列表中的元组,这有点复杂但也是可行的。