1

I have an array of variable numbers. I have another array that I want to be the of labels for the numbers array.

myList = [T1_sum, T2_sum, T3_sum, T4_sum, T5_sum, T6_sum, T7_sum, T8_sum, T9_sum, T10_sum]
mylables = ["cats", "dogs", "monkey", "fish", "rabbit", "owl", "pig", "goat", "cow", "bull"]

I want to sort the array by highest to lowest value. I did this by:

sortedlist = sorted(myList, reverse=True) 

However, how do I rearrange the labels so that they are in line with the values? I need to keep it so that a sortedlabellist is its own individual array (for future things). so i will probably have to link the values to the labels and then pull out a sortedlabellist separately?

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
kitchen800
  • 197
  • 1
  • 12
  • 36
  • 3
    By the looks of it, you should be using a dictionary structure - https://www.w3schools.com/python/python_dictionaries.asp – Joshua Nixon Apr 29 '21 at 21:10
  • 2
    turn it into a [dictionary](https://www.geeksforgeeks.org/python-convert-two-lists-into-a-dictionary/) & you can pull the dictionary 'labels' at anytime & put them into a list with `label_list = list(my_dict)` – DrBwts Apr 29 '21 at 21:20
  • 2
    As a more general rule, any time you have two data structures that need to be kept perfectly in sync, it's a very strong clue that they should be a single data structure. A dictionary is one option; another would be to make this a single list of tuples (or maybe `NamedTuple`s). Either of those approaches makes the sorting problem very easy. – Samwise Apr 29 '21 at 21:24
  • 3
    Does this answer your question? [Sorting list based on values from another list](https://stackoverflow.com/questions/6618515/sorting-list-based-on-values-from-another-list) – zoldxk Apr 29 '21 at 22:09

5 Answers5

5

You can just form a dictionary in order to maintain the order of both the lists while sorting:

>>> labelToSum = {key:value for key,value in zip(mylables, myList)}
>>> labelToSum
{'cats': 2,
 'dogs': 4,
 'monkey': 1,
 'fish': 7,
 'rabbit': 3,
 'owl': 8,
 'pig': 4,
 'goat': 5,
 'cow': 9,
 'bull': 4}

Then you can just sort this dictionary based on values, like this:

>>> {k: v for k, v in reversed(sorted(labelToSum.items(), key=lambda item: item[1]))}
{'cow': 9,
 'owl': 8,
 'fish': 7,
 'goat': 5,
 'bull': 4,
 'pig': 4,
 'dogs': 4,
 'rabbit': 3,
 'cats': 2,
 'monkey': 1}
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
  • 2
    This assumes the keys are unique (probably okay here). Also sorting and putting back into a dict fails prior to python 3.7/3.6 where dicts don't retain insertion order – Chris_Rands Apr 29 '21 at 21:27
  • You don't need the dictionary comprehension. Just do: `labelToSum = dict(zip(myTables, myList))` – Woodford Apr 29 '21 at 21:51
3

If you want to avoid using a dictionary you can use zip and unzip (*zip):

sortedList, sortedLables = zip(*sorted(zip(myList, mylables), reverse=True))

In fact, the unpacking returns the sequences as tuples. If you need it as list, you can cast it to list again:

sortedList, sortedLables = list(sortedList), list(sortedLables)
print(sortedList)
print(sortedLables)

# Output
['T9_sum', 'T8_sum', 'T7_sum', 'T6_sum', 'T5_sum', 'T4_sum', 'T3_sum', 'T2_sum', 'T1_sum', 'T10_sum']
['cow', 'goat', 'pig', 'owl', 'rabbit', 'fish', 'monkey', 'dogs', 'cats', 'bull']
Alex G
  • 663
  • 3
  • 13
1

You could use list comprehension and zip():

[x for _, x in sorted(zip(myList, mylables), reverse=True)]
Andreas
  • 8,694
  • 3
  • 14
  • 38
1

You can do this.

sortedlabellist = [x for _, x in sorted(zip(myList, mylables), reverse = True)]
1

The problem here is that the data structures you've choosen are not adecuate. If you just want to associate each element of myList with elements of mylables then a list of tuples [("cats", T1_sum), ("dogs", T2_sum), ...] should be enough.

Assuming both lists have same length (so as not to lose information),

zip(mylables,myList)

gets a list of tuples. Then you should sort it by first component.

Now, if you have more information about the lists, say every element in mylables (["cats", "dogs", ...]) appears only once, then choosing a dictionary (data structure in python) with mylables elements as keys and myList elements as values will make search operations and sorting operations more efficient.

Assuming both lists have the same length (so as not to lose information),

myDictionary = dict(zip(mylables,myList))

gets you a dictionary that can be sorted with the next line:

sorted(myDictionary)