Can I trust the dictate’s order to stay the same with every repetition?

I have the following three lines (they exist independently, but are displayed here together for convenience):

from mx2.x.org (mx2.x.org. [198.186.238.144]) by mx.google.com with ESMTPS id g34si6312040qgg.122.2015.04.22.14.49.15 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 22 Apr 2015 14:49:16 -0700 (PDT) from HQPAMAIL08.x.org (10.64.17.33) by HQPAMAIL13.xxorg (10.34.25.11) with Microsoft SMTP Server (TLS) id 14.2.347.0; Wed, 22 Apr 2015 17:49:13 -0400 from HQPAMAIL13.x.org ([fe80::7844:1f34:e8b2:e526]) by HQPAMAIL08.iadb.org ([fe80::20b5:b1cb:9c01:aa86%18]) with mapi id 14.02.0387.000; Wed, 22 Apr 2015 17:49:12 -0400 

I am looking to fill a dict with some values ​​based on the reverse (bottom) line order. In particular, for each row, I retrieve the IP address as a sorting index, and then the full string as a value.

Given that order is important, I decided to go with the lists and first did something like this (pseudocode, with the text above):

 IPs =[] fullStrings =[] for string in strings: IPs.append[$theIpAddressFoundInTheString] fullstrings.append[$theWholeString] 

resulting in the following two lists (again, just an illustration):

 IPs ['198.186.238.144', '10.64.17.33', 'fe80::7844:1f34:e8b2:e526'] fullstrings ['from mx2.x.org (mx2.x.org. [198.186.238.144]) by mx.google.com with ESMTPS id g34si6312040qgg.122.2015.04.22.14.49.15 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 22 Apr 2015 14:49:16 -0700 (PDT)', 'from HQPAMAIL08.x.org (10.64.17.33) by HQPAMAIL13.xxorg (10.34.25.11) with Microsoft SMTP Server (TLS) id 14.2.347.0; Wed, 22 Apr 2015 17:49:13 -0400', 'from HQPAMAIL13.x.org ([fe80::7844:1f34:e8b2:e526]) by HQPAMAIL08.x.org ([fe80::20b5:b1cb:9c01:aa86%18]) with mapi id 14.02.0387.000; Wed, 22 Apr 2015 17:49:12 -0400'] 

This worked fine until the moment, but now when I start filling in the dict values ​​with the values ​​in these lists (by hardcoded indexes), comparing them with the values ​​in other lists (again by hardcoded indexes), etc., Not only does debugging become a pain, but code becomes unstable.

I am starting to rewrite with dict (returning a dict where IP addresses are keys and full strings are values). Then I will perform operations such as:

 for k,v in myDictOfIpsAndStrings: anotherDict[$someHardcodedText] = k anotherDict[$otherHardcodedText] = v 

Here is my concern: Can I be sure that the dict, at any time when it repeats, will always be executed in the order in which the dict was created? If not, is this my only option to return to lists (and tedious and fragile length comparisons, assignments inherent in this), etc.?

I know that a dict is inherently unsorted. And I know the sorted function, but I don’t want to sort their keys in descending / ascending order, etc., Namely, to maintain (somehow) the order in which the dict was created.

+6
source share
2 answers

Can I be sure that a dict, at any time during the iteration, will always be executed in the order in which the dict was created?

No, a dict is unordered and will expose its order, however the concrete implementation decides.

 >>> d = {3: 'c', 2: 'b', 1: 'a'} >>> d {1: 'a', 2: 'b', 3: 'c'} 

See, right after creating the dict order has changed.

If you want a deterministic, controlled order, you can use collections.OrderedDict

 >>> from collections import OrderedDict >>> d = OrderedDict([(3, 'c'), (2, 'b'), (1, 'a')]) >>> d OrderedDict([(3, 'c'), (2, 'b'), (1, 'a')]) 

You can access OrderedDict in the conventions you used to

 >>> d[3] 'c' >>> d.get(3) 'c' 

Note that you do not need to insert all elements when creating. You can insert them one at a time if you want.

 >>> d = OrderedDict() >>> d[3] = 'c' >>> d[2] = 'b' >>> d[1] = 'a' >>> d[4] = 'd' >>> d OrderedDict([(3, 'c'), (2, 'b'), (1, 'a'), (4, 'd')]) 
+9
source

You should not rely on the iterative order of the dict. Only you can get stable and repeatable ordering by doing something like:

 for key in sorted(yourdict): more code here 

This will give you a stable order, but probably not the one you want.

Are you trying to use OrderedDict

+4
source

Source: https://habr.com/ru/post/988941/


All Articles