0

How would you go about displaying the order of the variables in python after using sort(). For an example:

A=3
B=1
C=2
D=[A,B,C]

D.sort() would be [1,2,3], but I want to see [B,C,A] somehow

bla
  • 1,840
  • 1
  • 13
  • 17
mtfort
  • 1
  • 2
    [Taken from this answer](https://stackoverflow.com/a/592816/9070959). If you want to do this you should be using a dictionary. – emsimpson92 Jun 21 '18 at 22:51
  • @emsimpson92 can't sort a dictionary. You *can* sort dictionary items however – user3483203 Jun 21 '18 at 22:52
  • @user3483203 You can actually, [you just have to do it a bit differently](https://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/) – emsimpson92 Jun 21 '18 at 22:53
  • `for key, value in sorted(d.items(), key=whatever):` works just fine. Or `od = OrderedDict(sorted(d.items(), key=whatever)`. For that matter, in Python 3.7+, dictionaries are guaranteed to retain insertion order, so you can just do `dd = dict(sorted(d.items(), key=whatever)`. – abarnert Jun 21 '18 at 22:54
  • @emsimpson92 that's not sorting a dictionary, that's getting a sorted view of its items. Nothing in that article changes anything about the dictionary – user3483203 Jun 21 '18 at 22:54
  • 1
    OP isn't trying to sort a dictionary though, they are trying to sort a list of letters. – stelioslogothetis Jun 21 '18 at 22:55
  • @abarnert is it correct to say that there isn't an in-place sort for dictionaries in Python? Or will that be possible in 3.7? – user3483203 Jun 21 '18 at 22:58
  • @user3483203 There is no in-place sort method, and there probably never will be (especially since the idea of adding `move_to_end` and friends from `OrderedDict` was rejected). – abarnert Jun 21 '18 at 23:06
  • @stybl, the letters are only references, it is actually sorting a list of integers – chickity china chinese chicken Jun 21 '18 at 23:07

2 Answers2

1

Doing this with independent variables just floating around would be extremely difficult. Why not use a dictionary instead?

myDict = {"A":3, "B":1, "C":2}
D = ["A", "B", "C"]
D.sort(key=lambda l:myDict[l])

Here we have each letter and its corresponding number in a dictionary. We have a list of letters out of order, which we call sort() on. Instead of using the default comparisons however, we provide our own sorting function in the form of an inline lambda function, which gets the number from the dictionary that matches each letter.

In case you aren't familiar with the lambda syntax, it is the same as doing this but more compact:

def customSort(l):
    return myDict[l]

D.sort(key=customSort)
stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
0

Using collections.OrderedDict, you can feed a list of key-value pairs sorted by value.

from collections import OrderedDict
from operator import itemgetter

d = {'A': 3, 'B': 1, 'C': 2}

res = OrderedDict(sorted(d.items(), key=itemgetter(1)))

# get keys ordered by value
print(res.keys())
odict_keys(['B', 'C', 'A'])

# get ordered values
print(res.values())
odict_values([1, 2, 3])
jpp
  • 159,742
  • 34
  • 281
  • 339