Re: I am newbie who can explain this code to me?
在 2016年9月20日星期二 UTC-4下午3:11:27,Terry Reedy写道: > 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, 38016226...@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 I read an old 3.2 library and didn't notice at that time. -- https://mail.python.org/mailman/listinfo/python-list
Re: I am newbie who can explain this code to me?
Nobody wrote: > On Tue, 20 Sep 2016 15:12:39 +0200, Peter Otten wrote: > >> because they don't build lists only to throw them away. > > The lists could have been avoided by using iterators, e.g. > > import itertools as it > keys = xrange(256) > vals = it.imap(chr, keys) > max(it.imap(operator.setitem, it.repeat(d), keys, vals)) > >> Also, creating a list of dicts or lists is a common gotcha because after >> >> outer = [[]] * 3 >> >> the outer list contains *the* *same* list three times, not three empty >> lists. > > But in this case, it's not a gotcha; Remember that the OP is a newbie > it's intentional and the author of the original example is trying to be clever, but in my eyes doesn't succeed. > that each iteration > operates upon the same dictionary. > > Essentially, [d]*len(keys) is a trick to get around the fact that map() > requires all of the arguments (apart from the function) to be sequences. > > itertools.repeat() is possibly a better trick, although then you can't use > map(), because map() iterates until *all* sequences are exhausted, > appending None values for shorter sequences. itertools.imap() terminates > once the shortest sequence is exhausted. > > In this specific case, a loop or comprehension would have been better. But > in situations where you don't control the iteration, the ability to coerce > something into a pre-determined iteration pattern is useful. You mean there is a situation where you'd actually recommend/use > max(it.imap(operator.setitem, it.repeat(d), keys, vals)) ? I'd like to see that use case. On second thought -- we might both settle on "Recommended by nobody" ;) -- https://mail.python.org/mailman/listinfo/python-list
Re: I am newbie who can explain this code to me?
On Tue, 20 Sep 2016 15:12:39 +0200, Peter Otten wrote: > because they don't build lists only to throw them away. The lists could have been avoided by using iterators, e.g. import itertools as it keys = xrange(256) vals = it.imap(chr, keys) max(it.imap(operator.setitem, it.repeat(d), keys, vals)) > Also, creating a list of dicts or lists is a common gotcha because after > > outer = [[]] * 3 > > the outer list contains *the* *same* list three times, not three empty > lists. But in this case, it's not a gotcha; it's intentional that each iteration operates upon the same dictionary. Essentially, [d]*len(keys) is a trick to get around the fact that map() requires all of the arguments (apart from the function) to be sequences. itertools.repeat() is possibly a better trick, although then you can't use map(), because map() iterates until *all* sequences are exhausted, appending None values for shorter sequences. itertools.imap() terminates once the shortest sequence is exhausted. In this specific case, a loop or comprehension would have been better. But in situations where you don't control the iteration, the ability to coerce something into a pre-determined iteration pattern is useful. -- https://mail.python.org/mailman/listinfo/python-list
Re: I am newbie who can explain this code to me?
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, 38016226...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: I am newbie who can explain this code to me?
在 2016年9月20日星期二 UTC-4上午9:13:35,Peter Otten写道: > 38016226...@gmail.com wrote: > > > 在 2016年9月20日星期二 UTC-4上午8:17:13,BartC写道: > >> On 20/09/2016 13:12, 38016226...@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. > >> > > >> > >> if len(keys) is 5 then [d]*5 is: > >> > >> [d,d,d,d,d] > >> > >> [d] is a list, containing one item, a dict if that is what it is. > >> > >> -- > >> Bartc > > > > Thank you. I understand now > > 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)} > > and even > > keys = range(256) > d = dict(zip(keys, map(chr, keys))) > > because they don't build lists only to throw them away. > > > Also, creating a list of dicts or lists is a common gotcha because after > > outer = [[]] * 3 > > the outer list contains *the* *same* list three times, not three empty > lists. Try > > outer[0].append("surprise") > print(outer) > > in the interactive interpreter to see why the difference matters. > > > Finally, if you are just starting you might consider picking Python 3 > instead of Python 2. Thank you.I learn more! -- https://mail.python.org/mailman/listinfo/python-list
Re: I am newbie who can explain this code to me?
38016226...@gmail.com wrote: > 在 2016年9月20日星期二 UTC-4上午8:17:13,BartC写道: >> On 20/09/2016 13:12, 38016226...@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. >> > >> >> if len(keys) is 5 then [d]*5 is: >> >> [d,d,d,d,d] >> >> [d] is a list, containing one item, a dict if that is what it is. >> >> -- >> Bartc > > Thank you. I understand now 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)} and even keys = range(256) d = dict(zip(keys, map(chr, keys))) because they don't build lists only to throw them away. Also, creating a list of dicts or lists is a common gotcha because after outer = [[]] * 3 the outer list contains *the* *same* list three times, not three empty lists. Try outer[0].append("surprise") print(outer) in the interactive interpreter to see why the difference matters. Finally, if you are just starting you might consider picking Python 3 instead of Python 2. -- https://mail.python.org/mailman/listinfo/python-list
Re: I am newbie who can explain this code to me?
在 2016年9月20日星期二 UTC-4上午8:17:13,BartC写道: > On 20/09/2016 13:12, 38016226...@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. > > > > if len(keys) is 5 then [d]*5 is: > > [d,d,d,d,d] > > [d] is a list, containing one item, a dict if that is what it is. > > -- > Bartc Thank you. I understand now -- https://mail.python.org/mailman/listinfo/python-list
Re: I am newbie who can explain this code to me?
On 20/09/2016 13:12, 38016226...@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. if len(keys) is 5 then [d]*5 is: [d,d,d,d,d] [d] is a list, containing one item, a dict if that is what it is. -- Bartc -- https://mail.python.org/mailman/listinfo/python-list