Ordered dictionary?

This is the place for queries that don't fit in any of the other categories.

Ordered dictionary?

Postby XndrK » Fri Sep 06, 2013 6:54 pm

I'm trying to make a program that prints out a dictionary in an ordered form, and I have this snippet of code from a previous project. (I was working on a program that would print the lyrics to the Ants go Marching song.)

Code: Select all
#Actions and order
action = {"one" : "suck his thumb",
    "two" : "tie his shoe",
    "three" : "take a pee",
    "four" : "shut the door",
    "five" : "take a dive",
    "six" : "pick up sticks",
    "seven" : "go to 7-11",
    }

#dictionary order kludge
verses = ["one", "two", "three", "four", "five", "six", "seven"]


The thing is, I'm trying to make a function that will take any dictionary and sort it.

My real question is, how do I access the keys in a dictionary?
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Ordered dictionary?

Postby XndrK » Fri Sep 06, 2013 7:30 pm

Solved my own problem by using for loop and appending.

Like this.

Code: Select all
def dictsorter(dct):
    keylist = []
    for i in dct:
        keylist.append(i)
    keylist.sort()
    for i in keylist:
        print str(i) + " : " + str(dct[i])

dct = {"spam" : 1.25,
    "eggs" : 1.55,
    "blue whale" : 2280.57
    }

dictsorter(dct)


Didn't know you could use for loops with dictionaries.

Thanks anyway. :)
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Ordered dictionary?

Postby stranac » Fri Sep 06, 2013 8:56 pm

You can just use sorted() with dict.items():
Code: Select all
>>> dct = {"spam" : 1.25,
...     "eggs" : 1.55,
...     "blue whale" : 2280.57
...     }
>>> for key, value in sorted(dct.items()):
...     print '{}: {}'.format(key, value)
...
blue whale: 2280.57
eggs: 1.55
spam: 1.25
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Ordered dictionary?

Postby micseydel » Fri Sep 06, 2013 10:24 pm

Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1203
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Ordered dictionary?

Postby XndrK » Wed Sep 11, 2013 10:59 pm

Oh. That's handy.
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm


Return to General Coding Help

Who is online

Users browsing this forum: Yoriz and 3 guests