[Tutor] Address book sort of

justinstraube at charter.net justinstraube at charter.net
Mon Dec 6 12:23:07 CET 2004


>> How do i pretty print output of dictionary container? Sort of tabular
>> form or something, e.g.,
>>
>> 1. name1        email address1
>> 2. name2        email address2
>>
>> Just for my learning experience :-). Thanks!

[Liam Clarke]

highLength=0
for element in myDict.keys():
     if len(element) > highLength:
          highLength = len(element)

index = 0
minimumSpaces= 5
for (key, item) in myDict.items():
      index += 1
      spaceMult=(highLength+minimumSpaces)-len(key)
      outString=str(index)+". "+key+(spaceMult * " ") + item
      print outString

[/Liam Clarke]

This is what I had come up with. Where 'd' is a dictionary. This also assumes 
that when the display_name is input, that it is less than 25 characters in 
length.

####
def display_contacts(d):
    print '\nYou have %i contacts in your book.\n' % len(d)
    while 1:
        count = 0
        for item in d:
            display_name = item
            while len(display_name) < 25:
                display_name += '.'
            count += 1
            print count, display_name, d[item]

        raw_input('\nPress <Return> to continue.\n')
        break
####
>>> x = {'Justin Straube': 'justinstraube at charter.net',
     'Eri Mendz': 'jerimed at myrealbox.com',
     'Python-Tutor': 'tutor at python.org',
     'Hello': 'World'}
>>> display_contacts(x)

You have 4 contacts in your book.

1 Justin Straube........... justinstraube at charter.net
2 Hello.................... World
3 Eri Mendz................ jerimed at myrealbox.com
4 Python-Tutor............. tutor at python.org

Press <Return> to continue.

>>>

regards,

Justin

---
Justin Straube
justinstraube at charter.net
http://www.angelfire.com/wi3/phosphorescent/

Whatever you have thought about the world before,
forget it, now you are in this one



More information about the Tutor mailing list