简体   繁体   English

检查字典值中是否存在列表元素

[英]Check if a List Element Exists in Dictionary Values

I have a list and a dictionary, and I need to check if elements of the list exist in the dictionary.我有一个列表和一个字典,我需要检查列表中的元素是否存在于字典中。 If yes, I need to copy the key-value pair to a new dictionary, and discard the rest from the dictionary.如果是,我需要将键值对复制到新字典中,并从字典中丢弃其余部分。

list0 = [
    -0.2385384148158066,
    -0.2307061452151448,
    -0.2150726345799602,
    -0.2138622652947372,
    -0.1098235264547504,
    -0.1072424768342444,
    -0.1037212327115436,
    -0.0966926943378552,
    -0.09614853460521154,
    9.353161396230564e-07
]
dict0 = {
    67: 1.440192237446893e-05,
    91: -0.1037212327115436,
    115: -0.2307061452151448,
    172: 0.0002879308975510175,
    242: 1.340170610273099e-05,
    266: -0.09614853460521154,
    290: -0.2138622652947372,
    347: 9.353161396230564e-07,
    417: 1.462739691486375e-05,
    441: -0.1072424768342444,
    465: -0.2385384148158066,
    513: 0.001208308075300354,
    606: -0.1098235264547504,
    630: -0.2442794187837403,
    677: 0.0007379497093922571,
    747: 1.336163063745514e-05,
    771: -0.0966926943378552,
    794: -0.2150726345799602
}

I tried iterating over the list and checking if the element is in dict0.values() and vice versa, but still couldn't figure out any way to access both key and value.我尝试遍历列表并检查元素是否在dict0.values() ,反之亦然,但仍然无法找到访问键和值的任何方法。

You can use a dict comprehension:您可以使用字典理解:

>>> {k: v for k, v in dict0.items() if v in list0}
{91: -0.1037212327115436, 115: -0.2307061452151448, 266: -0.09614853460521154, 290: -0.2138622652947372, 347: 9.353161396230564e-07, 441: -0.1072424768342444, 465: -0.2385384148158066, 606: -0.1098235264547504, 771: -0.0966926943378552, 794: -0.2150726345799602}

If your list0 is very big, converting it to a set makes the above method faster:如果您的list0非常大,将其转换为set会使上述方法更快:

>>> set0 = set(list0)
>>> {k: v for k, v in dict0.items() if v in set0}

Thats Pretty Simple to do:这很简单:

This snippet may help:这个片段可能有帮助:

new_dict={}
    for item in list0:
       for d in dict0:
          if(item==dict0[d]):
             new_dict[d]=item

I'm not able to figure out why didn't you get the answer by iteration.我无法弄清楚为什么你没有通过迭代得到答案。 You've not pasted your code, so I'm giving the answer in iteration method only so that you can compare it with your code.您还没有粘贴您的代码,所以我仅在迭代方法中给出答案,以便您可以将其与您的代码进行比较。 The code would be:代码将是:

list0 = [
    -0.2385384148158066,
    -0.2307061452151448,
    -0.2150726345799602,
    -0.2138622652947372,
    -0.1098235264547504,
    -0.1072424768342444,
    -0.1037212327115436,
    -0.0966926943378552,
    -0.09614853460521154,
    9.353161396230564e-07
]
dict0 = {
    67: 1.440192237446893e-05,
    91: -0.1037212327115436,
    115: -0.2307061452151448,
    172: 0.0002879308975510175,
    242: 1.340170610273099e-05,
    266: -0.09614853460521154,
    290: -0.2138622652947372,
    347: 9.353161396230564e-07,
    417: 1.462739691486375e-05,
    441: -0.1072424768342444,
    465: -0.2385384148158066,
    513: 0.001208308075300354,
    606: -0.1098235264547504,
    630: -0.2442794187837403,
    677: 0.0007379497093922571,
    747: 1.336163063745514e-05,
    771: -0.0966926943378552,
    794: -0.2150726345799602
}
new_dict={}
for i in dict0:
    if dict0[i] in list0:
        new_dict[i]=dict0[i]
print(new_dict)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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