-1
def positive(self):
    total = {}
    final = {}
    for word in envir:
        for i in self.lst:
            if word in i:
                if word in total:
                    total[word] += 1
                else:
                    total[word] = 1
    final = sorted(total, reverse = True)

    return total

This returns

{'climate': 10, 'ecosystem': 1, 'energy': 6, 'human': 1, 'world': 2, 'renewable': 2, 'native': 2}

I want to get this dictionary back to a dictionary that is in order. How do you I sort it and return a dictionary?

smci
  • 32,567
  • 20
  • 113
  • 146
  • 4
    Why do you want to sort a dictionary? You can't sort a dictionary. – Iluvatar Dec 11 '16 at 03:53
  • 3
    Use [```collections.OrderedDict```](https://docs.python.org/3/library/collections.html#collections.OrderedDict), I thnk in Python 3.6 *normal* dictionaries become ordered. – wwii Dec 11 '16 at 04:01
  • 1
    Did you want to return final? – OneCricketeer Dec 11 '16 at 04:04
  • Python does in fact have an OrderedDict. However it can't be sorted by arbitrary function, it only retains its original insertion order. A more complicated related question: [How to sort OrderedDict in OrderedDict - Python](https://stackoverflow.com/questions/8031418/how-to-sort-ordereddict-in-ordereddict-python)? – smci Dec 11 '16 at 04:11
  • Related: @JacquesdeHooge's answer to [Sorting OrderedDict not working](https://stackoverflow.com/posts/38796446/revisions) – smci Dec 11 '16 at 04:25

2 Answers2

1

An ordered dictionary would get you what you need

from collections import OrderedDict

If you want to order your items in lexicographic order, then do the following

d1 = {'climate': 10, 'ecosystem': 1, 'energy': 6, 'human': 1, 'world': 2, 'renewable': 2, 'native': 2}
od = OrderedDict(sorted(d1.items(), key=lambda t: t[0]))

Contents of od:

OrderedDict([('climate', 10),
             ('ecosystem', 1),
             ('energy', 6),
             ('human', 1),
             ('native', 2),
             ('renewable', 2),
             ('world', 2)])

If you want to specify exactly which order you want your dictionary, then store them as tuples and store them in that order.

t1 = [('climate',10), ('ecosystem', 1), ('energy',6), ('human', 1), ('world', 2), ('renewable', 2), ('native', 2)]
od = OrderedDict()

for (key, value) in t1:
    od[key] = value 

od is now

OrderedDict([('climate', 10),
             ('ecosystem', 1),
             ('energy', 6),
             ('human', 1),
             ('world', 2),
             ('renewable', 2),
             ('native', 2)])

In use, it is just like a normal dictionary, but with its internal contents' order specified.

Ébe Isaac
  • 11,563
  • 17
  • 64
  • 97
0

Dictionaries in Python have no explicit order (except in 3.6). There is no property of 'order' in a hash table. To preserve order in Python, use a list of tuples:

unordered = (('climate', 10,), ('ecosystem', 1)) # etc

Calling sorted(unordered) on the above will give it back with the 'key' being the first item in each individual tuple. You do not need to provide any other arguments to sorted() in this case.

To iterate, use for x, y in z: where z is the list.

Tatsh
  • 2,780
  • 1
  • 20
  • 23
  • The OP is clearly looking for `collections.OrderedDict`. Telling them that `dict` doesn't support key order isn't helpful. – smci Dec 11 '16 at 04:29