Posts: 3
Threads: 1
Joined: May 2018
May-10-2018, 10:26 AM
(This post was last modified: May-10-2018, 11:03 AM by buran.)
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
Posts: 136
Threads: 0
Joined: Apr 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 :)
Posts: 1,150
Threads: 42
Joined: Sep 2016
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?
Posts: 136
Threads: 0
Joined: Apr 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 :)
Posts: 3
Threads: 1
Joined: May 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.
Posts: 1,150
Threads: 42
Joined: Sep 2016
(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?
Posts: 566
Threads: 10
Joined: Apr 2017
(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"
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 1,150
Threads: 42
Joined: Sep 2016
(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 :)
Posts: 566
Threads: 10
Joined: Apr 2017
May-10-2018, 11:28 AM
(This post was last modified: May-10-2018, 11:30 AM by volcano63.)
(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
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 3
Threads: 1
Joined: May 2018
Thanks everyone for sparing time and explaining the concept
|