Python Forum
Please help in understanding this piece of code
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help in understanding this piece of code
#1
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
Reply
#2
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 :)
Reply
#3
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?
Reply
#4
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 :)
Reply
#5
(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.
Reply
#6
(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?
Reply
#7
(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.
Reply
#8
(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 :)
Reply
#9
(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.
Reply
#10
Thanks everyone for sparing time and explaining the concept
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with understanding the following code ClimbAddict 3 2,294 Oct-13-2019, 10:14 AM
Last Post: ClimbAddict

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020