I cannot find one example of somebody doing this, only sorting by the first item. Here is my code:

dict.sort()

for key in dict.keys():
      print key, "%8s"% dict[key]

I'm only getting an alphabetical sorting of the left side of the dictionary..

like this: a:4 c:3 b:12

a 4
b 12
c 3

I want it to be this:

c 3
a 4
b 12

well you could do something like this:

d = { "a":4, "c":3, "b":12 }

for v in sorted( d.values() ):
    for key in d:
        if d[ key ] == v:
            print key, v
            break

hope this helps :)

of here's another way

d = { "a":4, "c":3, "b":12 }
newD = {}
# Reverse dictionary, i.e. becomes { 4:"a", 3:"c", 12:"b" }
for key in d:
    newD[ d[ key ] ] = key

for v in sorted( d.values() ):
    print newD[ v ], v

there has to be a simpler way of doing this though :)

One more question, how can I print something next to the output without having it repeated over and over?

Dictionary values are accessed by their key. To allow for high speed key searches, the keys are in a hash order. So a dictionary sorted alphabetically by it's keys would make no sense and would defeat the whole dictionary idea.

Here are some way doh.

>>> d = { "a":4, "c":3, "b":12 }
>>> sorted(d.items(), key=lambda (v,k): (v,k))
[('a', 4), ('b', 12), ('c', 3)]
>>> sorted(d.items(), lambda x, y: cmp(x[1], y[1]))
[('c', 3), ('a', 4), ('b', 12)]

It's not supposed to show the same value, I only printed 90 as an example next to the last code, and it did this.

Do you think we can se your code based on output print screen?
If you want help you post your code or the part you think cause the problem.

Do you think we can se your code based on output print screen?
If you want help you post your code or the part you think cause the problem.

Well I know its this part:

http://i47.tinypic.com/wccpoj.jpg

Nothing is modified really, I just need the text to go to the right of the printed dictionary

Edited 7 Years Ago by persianprez: n/a

>>> sorted(d.items(), key=lambda (v,k): (v,k))

The above expression should read:

sorted(d.items(), key=lambda (k,v): (v,k))
#                             ^^^ k and v swapped

Alternatively, one could just use

sorted(d.items(), key=lambda (k,v): v)

Or for sorted printing:

for (k, v) in sorted(d.items(), key=lambda (k,v): v):
    print k, "%8s" % v

Edited 7 Years Ago by pythopian: n/a

Comments
nice thinking!

I've already gotten the loop set, I just need to find a way to print next to the output

Ok, I kind of figured it out, I assigned the other parts of the problem into dictionary, so now I have multiple values = to each key.

NOW, I need to know how to display the other values as well. Here is a sample dictionary: {'EQ TRI':[3," ",(2pi/3)]

And I'm still using the same method to display with the for loop. How do I display the new values?

Finally got it!! Well I got it a few hours ago, it just hit me, anyways, heres the way I accomplished it. As usual, you can download the whole program at my website. http://pedrumgolriz.com/index/?p=68

dict = {}
    dict[3] = 'Equilateral Triangle'
    dict[4] = 'Square'
    dict[5] = 'Regular Pentagon'
    dict[6] = 'Regular Hexagon'
    dict[7] = 'Regular Septagon'
    dict[8] = 'Regular Octagon'
    dict[9] = 'Regular Nonagon'
    dict[10] = 'Regular Decagon'
    
    

    #Begin loop for rows and columns
    for i in range(3,11):
        shape = dict[i]
        ext = 360/i
        asum = 180*(i-2)
        inte = 180*(i-2)/i
        diag = i*(i-3)/2
	print '%-27s %-5s %-11.3f %-12s %-11.3f %2s' % (shape,i, ext,asum,inte,diag)

Edited 7 Years Ago by persianprez: n/a

Finally got it!! Well I got it a few hours ago, it just hit me, anyways, heres the way I accomplished it. As usual, you can download the whole program at my website. http://pedrumgolriz.com/index/?p=68

dict = {}
    dict[3] = 'Equilateral Triangle'
    dict[4] = 'Square'
    ...

I see what you've been trying to accomplish, and I think dictionaries are not quite the right abstraction for it. It seems you need a sequence of "shape descriptors" of a kind. I'm not sure if you are acquainted with the concept of classes and objects already, but if yes - they would probably be the right answer here. Perhaps something like this:

class Shape:
    def __init__(self,  i,  name):
        self.name = name
        self.i = i
        self.ext = 360/i
        self.asum = 180*(i-2)
        self.inte = 180*(i-2)/i
        self.diag = i*(i-3)/2

shapes = [
    Shape(3, 'Equilateral Triangle'), 
    Shape(4, 'Square'), 
    Shape(5, 'Regular Pentagon'), 
    Shape(6, 'Regular Hexagon'), 
    Shape(7, 'Regular Septagon'), 
    Shape(8, 'Regular Octagon'), 
    Shape(9, 'Regular Nonagon'), 
    Shape(10, 'Regular Decagon'), 
    ]

print
for s in shapes:
    print '%-27s %-5s %-11.3f %-12s %-11.3f %2s'  % (
        s.name, s.i, s.ext, s.asum, s.inte, s.diag)

Yea, I knew that it wasn't the best method, but it was required for this project.

This question has already been answered. Start a new discussion instead.