Hello,
I didn't understand the functional approach with the loop'for'. Could you explain me that?
U = >>> [[x * y for x in range (3)] for y in range (3)]
U >>>
[[0, 0, 0], [0, 1, 2], [0, 2, 4]]
I tried to understand with print but it does not tell me much /> :
>>> print([i*j for j in range(3)])
[0, 2, 4]
>>> print([i*j for i in range(3)])
[0, 2, 4]
function range() and loop 'for'
Page 1 of 15 Replies  672 Views  Last Post: 21 January 2014  01:24 PM
Replies To: function range() and loop 'for'
#2
Re: function range() and loop 'for'
Posted 21 January 2014  12:34 PM
I'm not sure exactly what you're after here, but I can unpack this line a little.
What you have here is called a list comprehension  that's a good phrase to search up and read up on.
In fact, it's a nested list comprehension, so let's take the inner one to start with:
This evaluates to a list whose elements are x*y for some xs and ys. What are the xs? They are the elements of the range(3), which is to say, the list [0,1,2]. That is, they are a sequence of three elements, and x takes each of those values in turn. So what is x? x is not specified in the comprehension, so we have to get it from outside.
Let's look at the outer loop:
[?????? for y in range (3)]
This generates a list of ?????? for each value in the range (3), and for each of those somethings in the list, y is available as a value from that range. What is that mysterious thing? Well, it's just that inner comprehension  so now we know where y comes from.
So we can read this as "make me a list with one element for each value y in range(3), and make each of those elements a list, itself containing one element x*y for each value x in the range (3)"
You can read this as a special sort of for loop if you like:
list1 = []
for y in range (3):
list2 = []
for x in range(3):
list2.append(x*y)
list1.append(list2)
In general, a list comprehension can be rewritten as a for loop in this fashion, but it doesn't work the other way: you can't rewrite all for loops as list comprehensions.
What you have here is called a list comprehension  that's a good phrase to search up and read up on.
In fact, it's a nested list comprehension, so let's take the inner one to start with:
[x * y for x in range (3)]
This evaluates to a list whose elements are x*y for some xs and ys. What are the xs? They are the elements of the range(3), which is to say, the list [0,1,2]. That is, they are a sequence of three elements, and x takes each of those values in turn. So what is x? x is not specified in the comprehension, so we have to get it from outside.
Let's look at the outer loop:
[?????? for y in range (3)]
This generates a list of ?????? for each value in the range (3), and for each of those somethings in the list, y is available as a value from that range. What is that mysterious thing? Well, it's just that inner comprehension  so now we know where y comes from.
[[x * y for x in range (3)] for y in range (3)]
So we can read this as "make me a list with one element for each value y in range(3), and make each of those elements a list, itself containing one element x*y for each value x in the range (3)"
You can read this as a special sort of for loop if you like:
list1 = []
for y in range (3):
list2 = []
for x in range(3):
list2.append(x*y)
list1.append(list2)
In general, a list comprehension can be rewritten as a for loop in this fashion, but it doesn't work the other way: you can't rewrite all for loops as list comprehensions.
#3
Re: function range() and loop 'for'
Posted 21 January 2014  12:59 PM
Thanks for your explanation
if i have to create the matrix Z = [[1,2,3],[4,5,6]] (2 lines and 3 columns), and to use that special loop but my codes don't work good :
if i have to create the matrix Z = [[1,2,3],[4,5,6]] (2 lines and 3 columns), and to use that special loop but my codes don't work good :
>> squares = [] >>> for x in range(6): squares.append(x+1) Z = [squares] >>> Z [[1, 2, 3, 4, 5, 6]] >>> squares [1, 2, 3, 4, 5, 6]
>>> [[x+1 for x in range(6)]] [[1, 2, 3, 4, 5, 6]] >>> [[x+1 for x in range(6)]] [[1, 2, 3, 4, 5, 6]] >>> [[x+y for x in range(1,4)] for y in range(0,2)] [[1, 2, 3], [2, 3, 4]]
#4
Re: function range() and loop 'for'
Posted 21 January 2014  01:03 PM
That's a nice little puzzle. I'm going to let you work it out. Here's a hint: [1,2,3] and [4,5,6] are the same sort of progression. What sort of operator do you need here?
#5
Re: function range() and loop 'for'
Posted 21 January 2014  01:18 PM
i think that's the operator 'addition' to get this matrix Z = [[1,2,3],[4,5,6]]
>>> [x+1 for x in [1,2,3]] [2, 3, 4] >>> [x for x in [1,2,3]] [1, 2, 3] >>> [[x for x in [1,2,3]] for y in [4,5,6]] [[1, 2, 3], [1, 2, 3], [1, 2, 3]] >>> [x for x in [4,5,6]] [4, 5, 6] >>> [[x+y for x in [1,2,3]] for y in [4,5,6]] [[5, 6, 7], [6, 7, 8], [7, 8, 9]] >>> [[x+y for x in [1,2,3]] for y in range(2)] [[1, 2, 3], [2, 3, 4]]
#6
Re: function range() and loop 'for'
Posted 21 January 2014  01:24 PM
That's correct. You're almost there now  you just need to figure out how to express "the thing to add to each member of that range" (what you might call the "offset". You can do that in a few ways. For example, you can do it with an explicit list of two values, or you can do it arithmetically, or you could use a range() with the step parameter.
Page 1 of 1
