Python Forum
Compare Two Values in the Same Dict
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare Two Values in the Same Dict
#1
Code:
reg_key = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkList\\Profiles'
if ctypes.windll.shell32.IsUserAnAdmin():
    with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_key, 0, winreg.KEY_ALL_ACCESS) as k:
        ns = winreg.QueryInfoKey(k)[0]
        if ns > 1:
            subs = []
            ts_dict = {}
            for i in range(ns):
                subs.append(winreg.EnumKey(k, i))
                reg_key2 = reg_key + '\\' + subs[i]
                with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_key2, 0, winreg.KEY_ALL_ACCESS) as k2:
                    val = winreg.QueryValueEx(k2, 'DateCreated')
                    ts_dict[i] = get_time_stamp(val)
The dict ts_dict returns numeric keys with a list as the value for each key. What is the best way to compare these two lists (values)? The lists are timestamps ordered like so [<year>, <month>, <day of week>, <day>, <hour>, <minute>, <second>]. They are all numeric, so a real example would be [2018, 8, 3, 1, 10, 2, 16] which is today's date at 10:02:16AM.
How can I compare two or more lists like these (in the dict if possible--so as to not have to worry about writing them to variables) for the newest (or youngest) date?

-m
Reply
#2
>>> l1 = [2018, 8, 3, 1, 10, 2, 16]
>>> l2 = [2018, 8, 3, 1, 10, 2, 19]
>>> l1 == l2
False
>>> l3 = [2018, 8, 3, 1, 10, 2, 16]
>>> l1 == l3
True
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Ahh, haha. Simple. I feel a little sheepish. I'll just use > and <.

This is the second time you've helped me, @wavic, rep is deserving, methinks.

But, let me ask, is there a way to do this using methods/functions pertaining to dicts (I have looked through the docs)? Because the number of lists within the dict could vary--sometimes more, sometimes less--I'd like to not have to assign the lists to a separate variable because it could be dynamic--I could have 4 lists one time, 2 the next time. I don't think it's possible to dynamically assign a variable name outside of a dict (which is why I used a dict in the first place).

-m
Reply
#4
Comparing dicts?

>>> d1 = {'a': [1,2,3], 'b': [11,22,33]}
>>> d2 = {'a': [0,2,3], 'b': [11,22,33]}
>>> d1 == d2
False
>>> d3 = {'b': [11,22,33], 'a': [1,2,3]}
>>> d1 == d3
True
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
No, I mean comparing two values within the same dict; compare key1:value to key2:value.
d1 = {0 : [1, 2, 3], 1 : [1, 2, 4]}
So, compare 0 to 1? Their values, I mean.
Reply
#6
d1[0] == d1[1]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Easy, lol! I knew that. I just confused and mind gamed myself hehe.

Anywho, thanks @wavic and @ichabod801. This forum has been quite helpful to me.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need to compare 2 values in a nested dictionary jss 2 855 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Loop through values and compare edroche3rd 6 684 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
  Trying to compare string values in an if statement israelsattleen 1 541 Jul-08-2023, 03:49 PM
Last Post: deanhystad
  Compare variable with values in a file paulo79 1 1,104 Apr-22-2022, 03:16 AM
Last Post: Pedroski55
  dict class override: how access parent values? Andrey 1 1,629 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  Removing nan values from a dict tomtom 8 7,020 Oct-05-2021, 06:44 PM
Last Post: tomtom
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 3,977 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
Exclamation Compare values in a for loop. penahuse 1 2,366 Feb-22-2021, 07:01 AM
Last Post: buran
  Trouble with converting list , dict to int values! faryad13 7 3,750 Sep-04-2020, 06:25 AM
Last Post: faryad13
  Sort a dict in dict cherry_cherry 4 73,312 Apr-08-2020, 12:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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