3

I practice python recently. for a few days my script has stopped, because I'm struggling on a problem. I want to sort the items by both values which has in common. like an chain. as you can see lower.

what I have:

dic= {'A': [62, 17], 'B': [59, 60], 'C': [60, 61], 'D': [57, 58], 'E': [61, 62], 'F': [58, 59]}

what I want:

dic={'A': [62, 17], 'E': [31, 62],'C': [60, 31],'B': [3, 60],'F': [58, 3],'D': [57, 58]}

to see clearly:

'A': [62, 17]

'E': [*31*, 62]

'C': [60, *31*]

'B': [3, 60]

'F': [58, 3]

'D': [57, 58]

there is a simple method to sort them ? if the topic already exists, can you send me the link. i haven't found it. thank you in advance. nb. in all case, I know the first and the last value of the chain (17 and 57)

  • 7
    You can't sort dictionaries. Dictionaries are [unordered key-value pairs](https://docs.python.org/3/tutorial/datastructures.html#dictionaries). – Christian Dean Dec 04 '17 at 17:02
  • 1
    I have NO idea what rule you're using to sort that – bendl Dec 04 '17 at 17:04
  • 6
    But this is somehow more than sorting. Why do the values of `'B'` change? – Ma0 Dec 04 '17 at 17:05
  • @Ev.Kounis at least half of the values change, not just `'B'` – bendl Dec 04 '17 at 17:06
  • You seem to have `'A':[62,17]` in both dicts. But in the first you have `'B':[59,60]` and in the second there is `'B':[3,60]`. This seems to have changed and I cannot work out why. I see how there is a chain in the second dict, but how does it relate to the first? – quamrana Dec 04 '17 at 17:06
  • 3
    You should note that this is impossible in general (if I understand correctly what you're asking). Before anyone can provide a useful answer, you need to tell us more about these dictionaries. Is a unique sorting guaranteed? What happens if there are duplicates? This is a complex and fiddly problem; you need to tell us what assumptions we can make. – colopop Dec 04 '17 at 17:08
  • 1
    @Bilkokuya Yeah, I guess. If a dictionary contains only one element, you could _technically_ say it's sorted. But in my opinion, that's really a stretch. If a data structure is sorted, then it's elements are put in a certain order. But a data structure with one element can only be put in one particular order, therefore you can't sort it. – Christian Dean Dec 04 '17 at 17:13

2 Answers2

1

Given what others have said about the order of a dictionary and violating the posters desire to have a dictionary, I return a sorted list. I used the accepted answer at In Python, how do I index a list with another list?

d= {'A': [62, 17], 'B': [59, 60], 'C': [60, 61], 'D': [57, 58], 'E': [61, 62], 'F': [58, 59]}
myList = [max(value) for key,value in d.items()]
print(myList)
myIndex = sorted(range(len(myList)),key=lambda x:myList[x],reverse=True)
L = [(key,value) for key,value in d.items()]
[L[i] for i in myIndex]
Harlan Nelson
  • 1,394
  • 1
  • 10
  • 22
0

in fact i don't need a new dictionary. but a ordered list by the common value of the item. it must be look like that: list=[17,62,32,60,3,58,57]