Select to view content in your preferred language

Is it possible to sort a text file before creatinga dictionary?

1053
3
Jump to solution
08-09-2013 11:03 AM
ionarawilson1
Deactivated User
I am using a txt file to create a dictionary. However the dictionary is not sorted according to the txt file no matter the sequence of values in the txt file. Is there a way to sort the file or to sort the dictionary according alphabetically ascending (A to Z) or in the order it is in the file? Thanks!

file = open('D:\\ArcGISDATA\\SARS\\OfficeDomain_list.txt', 'r')  foresterdomaintxt = {}  for line in file.readlines():          x = line.split(",")         a = x[0]         b = x[1]         bstrip =x[1].strip('\n')         foresterdomaintxt=bstrip print foresterdomaintxt 


Here is the file:

Temple,TE San Angelo,SG San Augustine,ST Weslaco,WE


And here is what I get, no matter the order of items in the file:

{'Weslaco': 'WE', 'San Augustine': 'ST', 'Temple': 'TE', 'San Angelo': 'SG'}
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Esri Alum
Dictionaries in python are always unordered. Use an ordereddict if you're on Python 2.7, or something like this otherwise:


for key, value in sorted(foresterdomaintxt.iteritems()):     print key, value

View solution in original post

0 Kudos
3 Replies
JasonScheirer
Esri Alum
Dictionaries in python are always unordered. Use an ordereddict if you're on Python 2.7, or something like this otherwise:


for key, value in sorted(foresterdomaintxt.iteritems()):     print key, value
0 Kudos
T__WayneWhitley
Honored Contributor
That is great stuff, thanks Jason I will have to remember that when I upgrade Python.
With an earlier version, I have done something like the following, which is just a snippet from a bigger piece of code I have running on a CURRENT map doc:
from operator import itemgetter
sortSummaryAc = sorted(([a, b/43560.0] for a, b in summary.iteritems()), key = itemgetter(1), reverse = True)


Probably, this warrants a further explanation since that 2nd line likely doesn't win any awards for 'readability'.
'summary' is the dictionary targetted for sorting, if you can't tell.  That's read in and areas compiled for types of habitat from earlier in the code.  It was just easier for me to do it this way...but like you said, it's unsorted and I then needed to update text elements in my map in reverse sorted order (thought that'd be nice to see the higher values on top of the list, rather than 'mixed' order).

iteritems makes summary 'iterable' (as Jason showed in his post) or in other words so that I can loop on it with the in-place acreage conversion --- the really interesting thing is I'm sorting on values instead of dict keys (with itemgetter(1)) and of course sorting in reverse.  Then it's simply a matter of looping through the list and setting the text prop on the text elements...

So, yes, the point is you can use the dictionary to 'compile' keys/values; then it seems the sorting afterwards is just incidental.

Enjoy,
Wayne


EDIT:
...okay, maybe this explanation will work better:

I can define a dictionary with keys: a, b, c.
I can print the dictionary, which returns: a, c, b.
Finally I can reorder with a list of pairs (tuples):  c, a, b
>>> d = {'a':5,'b':1,'c':10}
>>> print d
{'a': 5, 'c': 10, 'b': 1}

>>> from operator import itemgetter
>>> sortd = sorted(d.iteritems(), key = itemgetter(1), reverse = True)
>>> print sortd
[('c', 10), ('a', 5), ('b', 1)]
>>>  
0 Kudos
ionarawilson1
Deactivated User
Thank you Jason! This is very helpful! Thank  you Wayne for the example and the explanation!!!;)
0 Kudos