I am newbie who can explain this code to me?

Terry Reedy tjreedy at udel.edu
Tue Sep 20 15:10:41 EDT 2016


On 9/20/2016 9:12 AM, Peter Otten wrote:

>> 在 2016年9月20日星期二 UTC-4上午8:17:13,BartC写道:
>>> On 20/09/2016 13:12, 380162267qq at gmail.com wrote:
>>>>>>> d = {}
>>>>>>> keys = range(256)
>>>>>>> vals = map(chr, keys)
>>>>>>> map(operator.setitem, [d]*len(keys), keys, vals)
>>>>
>>>> It is from python library. What does [d]*len(keys) mean?
>>>> d is the name of dict but put d in [] really confused me.

Where in which 'python library?  I cannot findI the above in 2.7 or 3.6 
stdlib.  The code should be replaced by

> It should be noted that the code above is really bad Python.
> Better alternatives are the simple loop
>
> d = {}
> for i in range(256):
>      d[i] = chr(i)
>
> or the dict comprehension
>
> d = {i: chr(i) for i in range(256)}

this.

-- 
Terry Jan Reedy





More information about the Python-list mailing list