Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 23rd, 2012, 08:53 PM
 evolvtyon
Registered User

Join Date: Nov 2012
Posts: 2
Time spent in forums: 43 m 2 sec
Reputation Power: 0
Ordering a diccionary

Hey guys, I'm new here so hi to everyone! Just got your self's another python newb :/ sorry..

Anyways, any help with the following will be much appreciated!

I got this code:

en_es = {'one':'uno',
'two':'dos',
'three':'tres',
'four':'quatro',
'five':'cinco}'

def write_diccionary(d):
temp = []
k = d.keys()
k = list(k)
temp = k[1]
k[1] = k[4]
k[4] = temp
for i in k:
print(i,':',d[i])

The idea is to take the dictionary above, and order it. The code is fixed for a single situation where I know the positions of the (key,value) pairs. However I would like generalize this. I made this attempt:

def write_diccionary_2(d):
for k in sorted(d): print(k,':', d[k])

however the output is:

>>> write_diccionary_2(en_es)

five : cinco
two : dos
four : cuatro
three : tres
one : uno

Can someone please shed some light on this?

THANKS!

#2
November 23rd, 2012, 09:50 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,124
Time spent in forums: 1 Month 3 Weeks 2 Days 4 h 37 m 27 sec
Reputation Power: 455
Your program works on my system. The python prompts are cluttered because I entered data in multi-line chunks.
Code:
```\$ python3
Python 3.2.3 (default, Oct 19 2012, 19:53:16)
[GCC 4.7.2] on linux2
>>> en_es = {'one':'uno',
'two':'dos',
'three':'tres',
'four':'quatro',
'five':'cinco'}

... ... ... ... >>> >>> >>> def write_diccionary_2(d):
for k in sorted(d): print(k,':', d[k])
... ...
>>> write_diccionary_2(en_es)
five : cinco
four : quatro
one : uno
three : tres
two : dos
>>> ```

You are aware of the typographical error in your post?

'five':'cinco}'
__________________
[code]Code tags[/code] are essential for python code!

#3
November 23rd, 2012, 09:57 PM
 evolvtyon
Registered User

Join Date: Nov 2012
Posts: 2
Time spent in forums: 43 m 2 sec
Reputation Power: 0
Quote:
 Originally Posted by b49P23TIvg Your program works on my system. The python prompts are cluttered because I entered data in multi-line chunks. Code: ```\$ python3 Python 3.2.3 (default, Oct 19 2012, 19:53:16) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> en_es = {'one':'uno', 'two':'dos', 'three':'tres', 'four':'quatro', 'five':'cinco'} ... ... ... ... >>> >>> >>> def write_diccionary_2(d): for k in sorted(d): print(k,':', d[k]) ... ... >>> write_diccionary_2(en_es) five : cinco four : quatro one : uno three : tres two : dos >>> ``` You are aware of the typographical error in your post? 'five':'cinco}'

that last typo was a copy error, it's ok in my code, but thanks.

About this last code, the problem is that I want the output to be (which I can achieve with the first code):

one : uno
two : dos
three : tres
four : quatro
five : cinco

Instead of what python is giving me...

sorry, should have mentioned it before..

#4
November 23rd, 2012, 10:13 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,124
Time spent in forums: 1 Month 3 Weeks 2 Days 4 h 37 m 27 sec
Reputation Power: 455
My answer was ridiculous. Sorry. You need an ordered dictionary.
Code:
```>>> import collections
>>> d = collections.OrderedDict()
>>> d['one']='eins'
>>> d['two']='zwei'
>>> d['three']='drei'
>>> d['four']='vier'
>>> import pprint
>>> pprint.pprint(d)
{'one': 'eins',
'two': 'zwei',
'three': 'drei',
'four': 'vier'}```
(pprint didn't impact the dictionary order)

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Ordering a diccionary