[英]find extra key in dict by value
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
?它们中的值是相同的,只是
new_ids
有 9 个,而 old_ids 有 10 个。如何找到new_ids
中存在的这个额外key
?
value of 5537179
in old_ids does not exist in new_ids
, so i need to get a key 10722773
5537179
中的 5537179 的值在new_ids
中不存在,所以我需要获取密钥10722773
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}
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 OUTPUT
[10722773]
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 Output
{10722773: 5537179}
There are more ways too.还有更多的方法。 Fastest way also
也是最快的方式
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 Output
{10722773: 5537179} # for both key value
[10722773] # for only key
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 OUTPUT
My way 0.010995300021022558
Akk way 0.028010500012896955
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.