0

I have dictionary with list of objects as values.

class Device_Terminals():
    """Class for define object for each device"""
    def __init__(self, device_name, source, drain):
        self.device_name = device_name
        self.source = source
        self.drain = drain

device = Device_Terminals(line[0], line[1], line[3])
if line[2] not in devices_dict:
        devices_dict[line[2]] = []
devices_dict[line[2]].append(device)

I want to return the key whose one of its objects has specific name, the dictionary looks like this

{A1:[ MNA1 X_N1 VSS, MPA1_1 X X_P1_1, MPA1_2 X X_P1_2, MPA1_3 X X_P1_3, MPA1_4 X X_P1_4, MPA1_5 X X_P1_5, MPA1_6 X X_P1_6, MPA1_7 X X_P1_7], 'A3': [MNA3 X_N1 VSS, MPA3_1 X_P2_1 VDD, MPA3_2 X_P2_2 VDD, MPA3_3 X_P2_3 VDD, MPA3_4 X_P2_4 VDD, MPA3_5 X_P2_5 VDD]}

each element of the lists is an object. how can I return the key whose value of the

 device_name == MPA3_3: 
   return key
M.salameh
  • 109
  • 7
  • Please explain better what you’re looking for? Like what is `line` and what exactly you mean by `object` I’m not sure I fully understand where you’re going with this question. – Jab Nov 12 '18 at 10:20
  • What does the dictionary `device_dict` look like? – Ananth Nov 12 '18 at 10:20
  • What is `device_dict`? You're question is not very clear. Please provide a reproductible example and a clear explanation of your issue, so other will be able to help you. – Antwane Nov 12 '18 at 10:20
  • I edited the question @Ananth – M.salameh Nov 12 '18 at 10:36
  • Can you please show the code that you use to construct the dictionary. Your data does not make sense as dicts have key value pairs. I don't see that in your update. Dicts are usually presented as `{key: value, key: value}`. – Ananth Nov 12 '18 at 10:38
  • Possible duplicate of [Get key by value in dictionary](https://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary) – boonwj Nov 12 '18 at 10:39
  • when I am trying to print it it looks like this {'A1': [<__main__.Device_Terminals instance at 0x7fce0b42ba70>, <__main__.Device_Terminals instance at 0x7fce0b42bb48>, <__main__.Device_Terminals instance at 0x7fce0b42bc20>] – M.salameh Nov 12 '18 at 10:50
  • This is a dictionary with only one key (`A1`) and whose value is a list of `Device_Terminal` objects: where is the dict data you'r showing saved? Is this one of the `source`, `device` or `drain` field? – toti08 Nov 12 '18 at 10:59

2 Answers2

1

You can create defaultdict from collections instead of checking if the key exists and then appending to it.

from collections import defaultdict
devices_dict = defaultdict(list)

To add to this dict:

device = Device_Terminals(line[0], line[1], line[3])
devices_dict[line[2]].append(device)

To search its values, you put a simple loop over the dictionary:

# your search term
search_val = 'device_name'
for key, devices in devices_dict.items():
    for device in devices:
        if search_val == device.device_name:
            print(key)
dalonlobo
  • 484
  • 4
  • 18
0

If you are trying to do dict lookup using its values. Try look at the solution of this previously answered question.

Get key by value in dictionary

boonwj
  • 356
  • 1
  • 10
  • but what if the value is object and I need specific attribute to compare with? @boonwj – M.salameh Nov 12 '18 at 10:50
  • Since values in the dictionary is not unique, you will need to loop through all values in the dictionary and get the data you need. – boonwj Nov 12 '18 at 10:58