Python Forum
need to compare 2 values in a nested dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need to compare 2 values in a nested dictionary
#1
i have a nested dictionary like below
from nested_lookup import nested_lookup

sample_dict = {
    "data": {
        "systemdata": [
            {
                "system": "1",
                "tempData": {
                    "temperature": "49C",
                    "temperatureThreshold": "70C"
                }
            },

            {
                "system": "2",
                "tempData": {
                    "temperature": None,
                    "temperatureThreshold": "-1C"
                },

            }
        ],
        "detaildata": [
            {
                "detail": "1",
                "tempData": {
                    "temperature": "42C",
                    "temperatureThreshold": "70C"
                }},
            {
                "detail": "2",
                "tempData": {
                    "temperature": "38C",
                    "temperatureThreshold": "60C"
                },

            }
        ]
    }
}
a = nested_lookup("temperature", sample_dict)
print(a)
b = nested_lookup("temperatureThreshold", sample_dict)
print(b)
i need to return True only if temperature <temperatureThreshold
i tried using nested lookup but i find it tough to compare the values as nested lookup returns a list of the values like below
Output:
['49C', None, '42C', '38C'] ['70C', '-1C', '70C', '60C']
how do i proceed?
Reply
#2
(Nov-29-2023, 08:30 PM)jss Wrote: i need to return True only if temperature <temperatureThreshold
The output that you want for the entire example dictionary is not clear. Can you explain it?
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Slight problem, you can't do this:

None < -1
You will get an error.

Also, all values are strings, you will need to deal with that if you want to compare the numerical values.

I would collect the values you want as a list of tuples, then work on that. Never heard of nested_lookup!

sample_dict = {
    "data": {
        "systemdata": [
            {
                "system": "1",
                "tempData": {
                    "temperature": "49C",
                    "temperatureThreshold": "70C"
                }
            },
 
            {
                "system": "2",
                "tempData": {
                    "temperature": None,
                    "temperatureThreshold": "-1C"
                },
 
            }
        ],
        "detaildata": [
            {
                "detail": "1",
                "tempData": {
                    "temperature": "42C",
                    "temperatureThreshold": "70C"
                }},
            {
                "detail": "2",
                "tempData": {
                    "temperature": "38C",
                    "temperatureThreshold": "60C"
                },
 
            }
        ]
    }
}

mydict = sample_dict['data']
mykeys = list(mydict.keys()) # ['systemdata', 'detaildata']
tups = []
for key in mykeys:    
    mylist = mydict[key]
    print('The data is here in mylist[i][tempData]', mylist)
    print(f'mylist is {len(mylist)} long')
    for i in range(len(mylist)):
        tempdict = mylist[i]['tempData']
        print('tempData is', tempdict)
        tup = (tempdict['temperature'], tempdict['temperatureThreshold'])
        tups.append(tup)

for t in tups:
    print(t)
The tups are:

Output:
('49C', '70C') (None, '-1C') ('42C', '70C') ('38C', '60C')
jss likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 8 347 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  Loop through values and compare edroche3rd 6 699 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
  Trying to compare string values in an if statement israelsattleen 1 564 Jul-08-2023, 03:49 PM
Last Post: deanhystad
  Printing specific values out from a dictionary mcoliver88 6 1,423 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  Compare variable with values in a file paulo79 1 1,124 Apr-22-2022, 03:16 AM
Last Post: Pedroski55
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 1,761 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  Nested dictionary acting strange Pedroski55 2 2,118 May-13-2021, 10:37 PM
Last Post: Pedroski55
  format the output from a nested dictionary. nostradamus64 9 4,597 May-03-2021, 04:45 PM
Last Post: nostradamus64
Lightbulb Python Nested Dictionary michaelserra 2 2,623 Apr-18-2021, 07:54 AM
Last Post: michaelserra
  Getting values from a dictionary brunolelli 5 3,604 Mar-31-2021, 11:57 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020