Python Forum

Full Version: Compare Two Values in the Same Dict
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
>>> 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
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
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
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.
d1[0] == d1[1]
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.