Python Forum
Please help in understanding this piece of code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Please help in understanding this piece of code (/thread-10042.html)



Please help in understanding this piece of code - tinkalgo - May-10-2018

How this code
np.array([range(i, i + 3) for i in [2, 4, 6]])
results in
Output:
array([[2, 3, 4], [4, 5, 6], [6, 7, 8]])
Thanks


RE: Please help in understanding this piece of code - ThiefOfTime - May-10-2018

you are building an array out of a list which is created using a list comprehension.
this specific list comprehension picks each number in [2, 4, 6], one at a time and using the range function. the range(lower_bound, upper_bound) function itself returns a list containing all numbers from lower_bound to upper_bound - 1. So your list will contain 3 sublists. for the first one range will be called with 2, since it is the first one in [2, 4, 6]. So range(2, 5) == [2, 3, 4]. The second list is made from i = 4, so that range(4, 7) will return [4, 5, 6]. And finally using i = 6 will result in range(6, 9) == [6, 7, 8]. so that your complete list will look like this: [[2, 3, 4], [4, 5, 6], [6, 7, 8]]. And out of that you are creating an array :)


RE: Please help in understanding this piece of code - j.crater - May-10-2018

I was thinking about the OP's question... Wouldn't a "normal" python session produce a list of 3 range objects when [range(i, i + 3) for i in [2, 4, 6]] is called?


RE: Please help in understanding this piece of code - ThiefOfTime - May-10-2018

range always produces an output of type list. The xrange function on the other hand does not ;) But nevertheless if a list containing 3 lists or 3 range objects is given to np.array() they will be converted to lists and a normal array is made :)


RE: Please help in understanding this piece of code - tinkalgo - May-10-2018

(May-10-2018, 10:46 AM)ThiefOfTime Wrote: you are building an array out of a list which is created using a list comprehension.
this specific list comprehension picks each number in [2, 4, 6], one at a time and using the range function. the range(lower_bound, upper_bound) function itself returns a list containing all numbers from lower_bound to upper_bound - 1. So your list will contain 3 sublists. for the first one range will be called with 2, since it is the first one in [2, 4, 6]. So range(2, 5) == [2, 3, 4]. The second list is made from i = 4, so that range(4, 7) will return [4, 5, 6]. And finally using i = 6 will result in range(6, 9) == [6, 7, 8]. so that your complete list will look like this: [[2, 3, 4], [4, 5, 6], [6, 7, 8]]. And out of that you are creating an array :)

Thanks you very much, You explained the concept in an excellent way.


RE: Please help in understanding this piece of code - j.crater - May-10-2018

(May-10-2018, 11:03 AM)ThiefOfTime Wrote: range always produces an output of type list. The xrange function on the other hand does not ;) But nevertheless if a list containing 3 lists or 3 range objects is given to np.array() they will be converted to lists and a normal array is made :)

So it is indeed a property of np.array(), that converts range objects to lists when passed to it?


RE: Please help in understanding this piece of code - volcano63 - May-10-2018

(May-10-2018, 10:51 AM)j.crater Wrote: I was thinking about the OP's question... Wouldn't a "normal" python session produce a list of 3 range objects when [range(i, i + 3) for i in [2, 4, 6]] is called?

Yes, it would - but I presume numpy does not have dtype of range - so it implicitly converts range to list of values. "Batteries included"


RE: Please help in understanding this piece of code - j.crater - May-10-2018

(May-10-2018, 11:23 AM)volcano63 Wrote:
(May-10-2018, 10:51 AM)j.crater Wrote: I was thinking about the OP's question... Wouldn't a "normal" python session produce a list of 3 range objects when [range(i, i + 3) for i in [2, 4, 6]] is called?

Yes, it would - but I presume numpy does not have dtype of range - so it implicitly converts range to list of values. "Batteries included"

Yeah, that's what I imagined must be going on. Thanks :)


RE: Please help in understanding this piece of code - volcano63 - May-10-2018

(May-10-2018, 11:03 AM)ThiefOfTime Wrote: range always produces an output of type list. The xrange function on the other hand does not ;) But nevertheless if a list containing 3 lists or 3 range objects is given to np.array() they will be converted to lists and a normal array is made :)

In Python 3, range produces object of type range, and xrange is deprecated.

In general, all Python2 functions/methods that would produce list - dict.items, zip, range, map, filter (list itself excluded) - produce generator-like objects in Python3


RE: Please help in understanding this piece of code - tinkalgo - May-10-2018

Thanks everyone for sparing time and explaining the concept