hoi777
hoi777

Reputation: 1

how to sort a dictionary in a dictionary by key

can you help me a little bit i'm stuck somewhere in my python code. i'm sorry if my english is bad, but i'll try my best.

dict = {2: {20.934144973754883: 'red'},
      8: {36.431842803955078: 'blue'},
      9: {27.323692321777344: 'blue'},
     15: {37.194934844970703: 'blue'},
     18: {19.550689697265625: 'red'}}
y = []
x = []

for key,value in dict.items():
    y.append(key)

    for val,color in value.items():
        x.append(val)
        c.append(color)

plt.scatter(sorted(x),sorted(y),color = c)

To make a line of the data i sorted the x and y. But then when i scattered my plot, the colors where wrong. I know it's because i didn't sorted the colors. But if i try to sort them i fail. Because of the variable value:

value = {20.934144973754883: 'red'}
        {36.431842803955078: 'blue'}
        {27.323692321777344: 'blue'}
        {37.194934844970703: 'blue'}
        {19.550689697265625: 'red'}

You can't sort this. please help me, i don't know what to do

Upvotes: 0

Views: 69

Answers (2)

pylang
pylang

Reputation: 44495

To make a line of the data i sorted the x and y. But then when i scattered my plot, the colors where wrong.

Your expected results are not clear, but I have the impression you interested in directly plotting the data.

I know it's because i didn't sorted the colors.

If you are only interested in plotting data from the dictionary, sorting is unnecessary as we can unpack the dictionary in parallel.

But if i try to sort them i fail.

Right now, you are unpacking parts of your data separately, which causes misalignment. You may be mistakenly tempted to sort the data to correct this misalignment.

Instead, try unpacking with for loops or a list comprehension (note d is used instead of the builtin dict variable):

import matplotlib.pyplot as plt

#%matplotlib inline

d = { 2: {20.934144973754883: 'red'},
      8: {36.431842803955078: 'blue'},
      9: {27.323692321777344: 'blue'},
     15: {37.194934844970703: 'blue'},
     18: {19.550689697265625: 'red'}}

data = [(x, y, c) for y, v in d.items() for x, c in v.items()]

for x, y, c in data:
    plt.scatter(x, y, color=c)

enter image description here

Alternatively, you can substitute the last two lines with the following, which unpacks all x, y, c data in parallel using zip:

xs, ys, cs = zip(*data)
plt.scatter(xs, ys, color=cs)

Upvotes: 0

Implementing quantik's suggestion to use collections.OrderedDict makes sorting the keys possible and may yield the desired result:

import collections

d = collections.OrderedDict()

d[2]  = {20.934144973754883: 'red'}
d[8]  =  {36.431842803955078: 'blue'}
d[9]  = {27.323692321777344: 'blue'}
d[15] = {37.194934844970703: 'blue'}
d[18] = {19.550689697265625: 'red'}

y = []
x = []

for key,value in d.items():
    y.append(key)

    for val,color in value.items():
        x.append(val)
        c.append(color)

plt.scatter(sorted(x),sorted(y),color = c)

produces this output:

colors scatter plot

Upvotes: 1

Related Questions