简体   繁体   中英

Find which element of list is a key in a dictionary and what is it's value

I have a dictionary:

classes_dictionary = {'/m/09x0r': 'Speech', '/m/03qc9zr': 'Screaming'}

and a list:

labels_list = ['/m/03k3r', '/m/04rlf', '/m/07q5rw0', '/m/09x0r', '/m/0jbk']

labels_list will always contain at least one element which is a key of classes_dictionary . I wish to extract which classes are those with the lowest computational complexity. In this example, '/m/09x0r' will be translated into 'Speech' . my solution:

class_str = list()
for k in labels_list:
    print(k)
    if k in self.classes_dictionary:
        class_str.append(self.classes_dictionary[k])

I do not mind if the output is a list or any other type. Also, for generality of the question I am assuming only a single element of labels_list is a key, though the best answer may consider both cases.

Is there a more efficient way to implement this? I am asking on both, implementation efficiency

You can use get() and check if the returned value is not None instead of looking if the key exists in the dict (although it's O(1) operation). If you know it's only one value add break

class_str = []
for k in labels_list:
    value = classes_dictionary.get(k)
    if value:
        class_str.append(value)

If you are using Python 3.8 you can use Assignment Expressions , which will evaluate classes_dictionary.get(x) only once

class_str = [y for x in labels_list if (y := classes_dictionary.get(x)) is not None]

Using a list comprehension:

>>> [classes_dictionary[k] for k in labels_list if k in classes_dictionary]
['Speech']

If only one match is expected, you can use next with a generator expression to stop searching once it's found:

>>> next(classes_dictionary[k] for k in labels_list if k in classes_dictionary)
'Speech'

To be for efficent check key on dict.

#YOUR CODE
if k in self.classes_dictionary:
    #do_job

#More efficient
try:
    self.classes_dictionary[k]
    #do_job
except:
    #Key not in dict
    pass

Currently your labels_list consists of one string. It seems it should be a list of 5 strings. You do not need self. in front of classes_dictionary:

class_str = list()
for k in labels_list:
  print(k)
  if k in classes_dictionary:
    class_str.append(classes_dictionary[k])

If labels_list is a set instead, you might be able to squeeze out some performance. Use intersection of set and dictionary keys:

classes_dictionary = {'/m/09x0r': 'Speech', '/m/03qc9zr': 'Screaming'}
labels_list= set(['/m/03k3r', '/m/04rlf', '/m/07q5rw0', '/m/09x0r', '/m/0jbk'])

[classes_dictionary[k] for k in labels_list & classes_dictionary.keys()]

you could use the intersection of the dict keys with the elements from labels_list in a list comprehension:

class_str = [classes_dictionary[e] for e in set(classes_dictionary).intersection(labels_list)]

output:

['Speech']

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