简体   繁体   中英

How to get keys of dict A after performing set operation set(A.values()) - set(B.values()) cause their all keys are different but few values are same

A = {1:('a','b','c','d'), 2:('a','b','d','d'),3:('a','b','c','c')}

B = {111:('a','b','c','d'), 31:('a','b','d','d'),39:('a','b','c','e')}

#After performing.      
data = set(B.values()) - set(A.values())
#i am getting data = ('a','b','c','e')
#But my desired output is 
39 : ('a','b','c','e')'

I can't use B.items() as the keys are different. I can use nested for loops but I have around 700k+ data and using that approach is taking a lot Of time. Actually I'm syncing two mssql Tables using python which has 700k+ data and each row has 52+ values, earlier we used to truncate the table B and insert all data from table A, but it was taking long time. What we came up with is we did AB and then BA, then inserting all the AB rows and deleting all BA rows. But
a query with 52 parameters taking long time in deletion, so I came up with this idea to delete all the values with specific keys, but I'm unable to get the keys.

You rather need a dictionary comprehension:

S = set(A.values())
out = {k:v for k,v in B.items() if v not in S}

Output:

{39: ('a', 'b', 'c', 'e')}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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