-2

Input:

a = {'555': ['350975553655799810'], '333: ['234000479699468290'], '488': ['298803528183447552', '298069263703474177', '292721536517931008', '252858117883297792', '376836317469736961', '476413939975192578']}

Out:

a = {'488': ['298803528183447552', '298069263703474177', '292721536517931008', '252858117883297792', '376836317469736961', '476413939975192578'], '555': ['350975553655799810'], '333: ['234000479699468290']}

Sort the dictionary, by value, from the largest to the smallest array. Help !

  • 1
    What have you tried? What specifically do you need help with? – Carcigenicate Dec 10 '18 at 15:06
  • Can you add some clarification? Are you trying to move the arrays to different locations in the dictionary (ie, from being the value at the 555 key to being the value at the 488 key)? If you are trying to reorder the dictionary, that doesn't make any sense. A dictionary uses a key-value pair system. It is not ordered by index the same way an array is. – jaredad7 Dec 10 '18 at 15:07
  • I need to sort the dictionary by the number of elements in the array. I tried this option: `list_of_sorted_pairs = [(k, dictionary[k]) for k in sorted(dictionary.keys(), key=dictionary.get, reverse=True)]` – ni_Gusak Dec 10 '18 at 15:10
  • "I need to sort the dictionary by the number of elements in the array." What does this mean? Are you re-arranging the arrays to be associated with different keys? – jaredad7 Dec 10 '18 at 15:11
  • I need the dictionary to be arranged in such a way that the first pair, the key: the value, the value of the array length must be the largest and so in order. – ni_Gusak Dec 10 '18 at 15:13
  • The main sort is descending by the values of the lengths of the array, the key is the main one that would save its bundle with the value. – ni_Gusak Dec 10 '18 at 15:15
  • The linked duplicate doesn't really answer OP's question. OP wants a new dictionary, not to print the keys in order of the size of their paired values. – jaredad7 Dec 10 '18 at 15:53

1 Answers1

0

I don't think you're using the dictionary correctly, unless I am misunderstanding the question. I've tried asking for clarification in the comments, but that doesn't seem to be helping.

Dictionaries work on a key-value pair system. This means that you use a key, and not a traditional index (0, 1, 2, ...) to access the value.

For example, say that we have a dictionary call d

{ 'fruits': ['apple', 'banana', 'orange'], 'vegetables': ['carrot', 'potato'] }

In this dictionary, we access the array ['apple', 'banana', 'orange'] by calling d['fruits'].

We would not talk about "ordering" the dictionary in the sense that we did sometime like this:

{ 'vegetables': ['carrot', 'potato'], 'fruits': ['apple', 'banana', 'orange'] }

That just wouldn't make any sense given the data type.


You may be asking about something different. If what you want to do is something like the following:

Input:

{ '1': ['x', 'y', 'z'], '2': ['x'], '3': ['x', 'y'] }

Output:

{ '1': ['x'], '2': ['x', 'y'], '3': ['x', 'y', 'z'] }

the way you would do this is by first creating an array of the keys, then an array of of the elements:

keys = list(d.keys())
elements = list()
for k in keys:
    elements.append(d[k])

Then you will sort both of these using a sorting algorithm of your choice. Once these have both been sorted, you will iterate through the keys list and add the corresponding values to the original dictionary.

for i in range(len(keys)):
    d[keys[i]] = elements[i]

This will give you a new dictionary with the smallest key being paired with the smallest (by length) list. Optionally, sort the elements array in the opposite way from the keys array to have the largest key being paired with the smallest list.

jaredad7
  • 998
  • 2
  • 11
  • 33
  • You are on the right track. I need such a sort, just so that it is descending, and the keys remain the same for values. – ni_Gusak Dec 10 '18 at 15:33
  • I am trying to tell you that there is no way to sort a dictionary AND keep the keys the same for the values. That simply isn't how a dictionary works! – jaredad7 Dec 10 '18 at 15:34
  • I think there is a solution, just not obvious. – ni_Gusak Dec 10 '18 at 15:36