I have to dicts like this
new_ids = {10722774: 5537170, 10722775: 5537171, 10722776: 5537172, 10722777: 5537173, 10722778: 5537174, 10722779: 5537175, 10722780: 5537176, 10722781: 5537177, 10722782: 5537178}
old_ids = {10722773: 5537179, 10722764: 5537170, 10722765: 5537171, 10722766: 5537172, 10722767: 5537173, 10722768: 5537174, 10722769: 5537175, 10722770: 5537176, 10722771: 5537177, 10722772: 5537178}
values in them are the same, just new_ids
have 9 of them and old_ids have 10. how is to find this extra key
that exists in new_ids
?
value of 5537179
in old_ids does not exist in new_ids
, so i need to get a key 10722773
CodePudding user response:
Given the clarification:
new_ids = {
10722774: 5537170,
10722775: 5537171,
10722776: 5537172,
10722777: 5537173,
10722778: 5537174,
10722779: 5537175,
10722780: 5537176,
10722781: 5537177,
10722782: 5537178,
}
old_ids = {
10722773: 5537179,
10722764: 5537170,
10722765: 5537171,
10722766: 5537172,
10722767: 5537173,
10722768: 5537174,
10722769: 5537175,
10722770: 5537176,
10722771: 5537177,
10722772: 5537178,
}
# Flip key-value to value-key
flipped_old = {v: k for k, v in old_ids.items()}
flipped_new = {v: k for k, v in new_ids.items()}
flipped_combined = flipped_old | flipped_new
# Find keys that aren't shared between the flipped dicts
uncommon_keys = set(flipped_old) ^ set(flipped_new)
# Generate a dict that unflips the key-values containing only the uncommon keys
uncommon = {flipped_combined[v]: v for v in uncommon_keys}
print(uncommon)
prints
{10722773: 5537179}
CodePudding user response:
First way
_1. only key
new_ids = {10722774: 5537170, 10722775: 5537171, 10722776: 5537172, 10722777: 5537173, 10722778: 5537174, 10722779: 5537175, 10722780: 5537176, 10722781: 5537177, 10722782: 5537178}
old_ids = {10722773: 5537179, 10722764: 5537170, 10722765: 5537171, 10722766: 5537172, 10722767: 5537173, 10722768: 5537174, 10722769: 5537175, 10722770: 5537176, 10722771: 5537177, 10722772: 5537178}
notLst = [k for k,v in old_ids.items() if v not in new_ids.values()]
print(notLst)
OUTPUT
[10722773]
_2. both key and value
new_ids = {10722774: 5537170, 10722775: 5537171, 10722776: 5537172, 10722777: 5537173, 10722778: 5537174, 10722779: 5537175, 10722780: 5537176, 10722781: 5537177, 10722782: 5537178}
old_ids = {10722773: 5537179, 10722764: 5537170, 10722765: 5537171, 10722766: 5537172, 10722767: 5537173, 10722768: 5537174, 10722769: 5537175, 10722770: 5537176, 10722771: 5537177, 10722772: 5537178}
notLst = {k:v for k,v in old_ids.items() if v not in new_ids.values()}
print(notLst)
Output
{10722773: 5537179}
There are more ways too. Fastest way also
Second Way
You can use a set way too to solve the problem
new_ids = {10722774: 5537170, 10722775: 5537171, 10722776: 5537172, 10722777: 5537173, 10722778: 5537174, 10722779: 5537175, 10722780: 5537176, 10722781: 5537177, 10722782: 5537178}
old_ids = {10722773: 5537179, 10722764: 5537170, 10722765: 5537171, 10722766: 5537172, 10722767: 5537173, 10722768: 5537174, 10722769: 5537175, 10722770: 5537176, 10722771: 5537177, 10722772: 5537178}
# n = set.symmetric_difference(set(new_ids.values()),set(old_ids.values()))
n = set.symmetric_difference(*map(set,(new_ids.values(),old_ids.values())))
notLst = {k:v for k,v in old_ids.items() if v in n} # both key Value
# notLst = [k for k,v in old_ids.items() if v in n] # Only key
print(notLst)
Output
{10722773: 5537179} # for both key value
[10722773] # for only key
Time difference between my second way and the @AKK way
from timeit import timeit
new_ids = {10722774: 5537170, 10722775: 5537171, 10722776: 5537172, 10722777: 5537173, 10722778: 5537174, 10722779: 5537175, 10722780: 5537176, 10722781: 5537177, 10722782: 5537178}
old_ids = {10722773: 5537179, 10722764: 5537170, 10722765: 5537171, 10722766: 5537172, 10722767: 5537173, 10722768: 5537174, 10722769: 5537175, 10722770: 5537176, 10722771: 5537177, 10722772: 5537178}
def myway():
# n = set.symmetric_difference(set(new_ids.values()),set(old_ids.values()))
n = set.symmetric_difference(*map(set,(new_ids.values(),old_ids.values())))
notLst = {k:v for k,v in old_ids.items() if v in n} # both key Value
# notLst = [k for k,v in old_ids.items() if v in n] # Only key
return notLst
def AKKway():
flipped_old = {v: k for k, v in old_ids.items()}
flipped_new = {v: k for k, v in new_ids.items()}
flipped_combined = flipped_old | flipped_new
# Find keys that aren't shared between the flipped dicts
uncommon_keys = set(flipped_old) ^ set(flipped_new)
# Generate a dict that unflips the key-values containing only the uncommon keys
uncommon = {flipped_combined[v]: v for v in uncommon_keys}
return (uncommon)
print("My way",timeit(myway,number=10000))
print("Akk way",timeit(AKKway,number=10000))
OUTPUT
My way 0.010995300021022558
Akk way 0.028010500012896955
CodePudding user response:
I suggest making a condition to find the extra key within the new_ids.
Note: I am just trying to comment since I do not have a 50 reputation