Python: using Map


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # build a dictionary that maps the ordinals from 32 to 255
  2. # to their ASCII character equivalents eg. 33: '!'
  3. # (note that 32 and 160 are spaces)
  4. # Python24 by vegaseat 10may2005
  5.  
  6. import operator
  7.  
  8. print "ASCII values of characters:"
  9. print '-'*27 # print 27 dashes, cosmetic
  10.  
  11. # create an empty dictionary
  12. charD = {}
  13. # set keys from 32 to 255
  14. keyList = range(32, 256)
  15. # use map() to create the character list
  16. # applies function chr() to every item of the list
  17. valueList = map(chr, keyList)
  18. # use map() to form a dictionary from the two lists
  19. map(operator.setitem, [charD]*len(keyList), keyList, valueList)
  20.  
  21. print "%6s%6s%6s" % ("hex", "dec", "char")
  22. # print the dictionary one associated pair each line
  23. for key,value in charD.items():
  24. print "%6x%6d%6c" % (key, key, value)
  25.  
  26. print '-'*27

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.