-1

I'm new to python and I've been trying to sort some variables and then print the names of the variables in sorted order, but I can't get further than this.

x = 0
y = 9
z = 3

numbers = [x,y,z]
numbers.sort(reverse=True)

for x in range(len(numbers)):
    print(numbers[x])

I want it to print

y
z
x

But I don't know how to do this and I cant seem to find anything online

  • 1
    If you need the names, then you need to store them as strings. – Tom Karzes Sep 13 '21 at 18:54
  • If you need theses variable names in the output, you should be using a dictionary or some other data structure to store the values. Consider that list doesn't have to contain named variables (i.e. `numbers = [1, 2, 3]`) – Mark Sep 13 '21 at 18:54
  • @Mark from the question it appears they care about ordering. This makes it a little more complicated if they're not using Python3.7+. – BTables Sep 13 '21 at 18:57
  • 3
    The list knows nothing about the original variables; only the *values* were stored in the list. – chepner Sep 13 '21 at 18:58
  • One way to access your variables and their values is to call `vars()`, which will return a dictionary of your variables (among other things). You may want to consider sorting and returning a dict to begin with anyway `>>> variables = {var:value for var, value in vars().items() if var in ('x','y','z')}` which returns for `variables` `{'x': 0, 'y': 9, 'z': 3}` – Joe Carboni Sep 13 '21 at 18:59
  • @BTables If the OP is just starting, there's little reason for them to be using anything older than 3.7. – chepner Sep 13 '21 at 18:59
  • @chepner I agree they *should* be using something newer. Should is a dangerous word and assumption, though. – BTables Sep 13 '21 at 19:00
  • If you want to keep track of a list of values, have them stay in a consistent order, and have a programmatically accessible name for each value, then it sounds like you need an `OrderedDict`: https://docs.python.org/3/library/collections.html#collections.OrderedDict – Random Davis Sep 13 '21 at 19:04
  • @BTables I'm assuming nothing. I'm saying that if the OP isn't using anything newer, they should stop now and *get* something newer. – chepner Sep 13 '21 at 19:06
  • @chepner agreed there, too. I was just adding context for why I left my original comment. – BTables Sep 13 '21 at 19:09

4 Answers4

2

First, if you care about names associated with each value, you should be using a dict, not individual variables. Variable names are not data.

numbers = {'x': 0, 'y': 9, 'z': 3}

You can get the output you want by sorting the keys of the dict, but making comparisons based on the value associated with each key using the key argument to sorted.

for k in sorted(numbers, key=numbers.get, reversed=True):
    print(k)

When deciding if a value v1 is less than v2, sorted will compare them using numbers.get: v1 < v2 if numbers.get(v1) < numbers.get(v2).

chepner
  • 497,756
  • 71
  • 530
  • 681
  • `sorted` argument is not needed in the `sorted()` function. pretty sure it would error out. Other than that this answer is correct. – db702 Sep 13 '21 at 19:09
  • That's weird. I meant to type `reversed`. My fingers had other ideas, apparently. – chepner Sep 13 '21 at 19:12
0

As already mentioned in the comments a list knows nothing about your variable names. If you care about the names you can use a dictionary which has a key-value structure. For instance you can use:

d = {'x' : 0, 'y':9, 'z' : 4}

d_sorted = {k: v for k, v in sorted(d.items(), key=lambda item: 
item[1],reverse=True)}

To produce the following result:

{'y': 9, 'z': 4, 'x': 0}

Have also a look at where i got this code from.

msbet
  • 46
  • 6
0
dic ={'x':0,'y':9,'z':3}

for x in sorted( dic.items(), reverse=True, key=lambda item: item[1]):
    print(x[0])
Beso
  • 1,176
  • 5
  • 12
  • 26
-1

Use a dictionary to store the variable names and their values:

var_info = {"x": 0, "y": 9, "z": 3}

Then use a loop over the sorted items:

import operator

for item in sorted(var_info.items(), key=operator.itemgetter(1), reverse=True):
    print(item[1])
Alan Bagel
  • 818
  • 5
  • 24