The map() function applies a function to every member of a list. Here a list of numbers 32 to 255 is changed to a list of the corresponding ASCII characters. Then map() is used in a more complex application to combine the two lists to form a dictionary containing number:character pairs.
Some examples of the map function (Python)
# build a dictionary that maps the ordinals from 32 to 255
# to their ASCII character equivalents eg. 33: '!'
# (note that 32 and 160 are spaces)
# Python24 by vegaseat 10may2005
import operator
print "ASCII values of characters:"
print '-'*27 # print 27 dashes, cosmetic
# create an empty dictionary
charD = {}
# set keys from 32 to 255
keyList = range(32, 256)
# use map() to create the character list
# applies function chr() to every item of the list
valueList = map(chr, keyList)
# use map() to form a dictionary from the two lists
map(operator.setitem, [charD]*len(keyList), keyList, valueList)
print "%6s%6s%6s" % ("hex", "dec", "char")
# print the dictionary one associated pair each line
for key,value in charD.items():
print "%6x%6d%6c" % (key, key, value)
print '-'*27
delocalizer 0 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
delocalizer 0 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.