简体   繁体   中英

Understanding For loop execution in Python 3.7

I have executed three different for loop operations in Jupyter notebook. Code is given below

First for loop

b = {}
temp_1 = []
for x in range(0,4):
    y = [4,5]
    temp_1.append(x*y)
b[x] = temp_1
print("\n First for loop output \n",b)

Second for loop

b = {}
temp_1 = []
for x in range(0,4):
    for y in range(4,6):
        temp_1.append(x*y)
    b[x] = temp_1
print("\n 2nd for loop output \n",b)

Third for loop

b = {}
temp_1 = []
for x in [0,1,2,3]:
    for y in [4,5]:
        temp_1.append(x*y)
    b[x] = temp_1
print("\n 3rd for loop output \n",b)

Output of First for loop output

{0: [[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]], 1: [[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]], 2: [[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]], 3: [[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]]}

2nd for loop output

{0: [0, 0, 4, 5, 8, 10, 12, 15], 1: [0, 0, 4, 5, 8, 10, 12, 15], 2: [0, 0, 4, 5, 8, 10, 12, 15], 3: [0, 0, 4, 5, 8, 10, 12, 15]}

3rd for loop output

{0: [0, 0, 4, 5, 8, 10, 12, 15], 1: [0, 0, 4, 5, 8, 10, 12, 15], 2: [0, 0, 4, 5, 8, 10, 12, 15], 3: [0, 0, 4, 5, 8, 10, 12, 15]}

Q1: I am beginner to Python. I have been doing simple coding in MATLAB C and Arduino UNO controller. Literally, I am failing to understand how for loop works in Python. Particularly, what is happening in First for loop and second and third.

Q2: I wanted the output something like {0:[0,0], 1:[4,5],2:[8,10],3:[12,15]} . Could you please help me with proper code which can fetch me this output.

Try this:

b = {}
for x in [0,1,2,3]:
    temp_1 = []
    for y in [4,5]:
        temp_1.append(x*y)
    b[x] = temp_1
print("\n 3rd for loop output \n",b)

In your first loop, you are multiplying a list [4,5] which is not what you want because it is not element-wise. Read http://thepythonguru.com/python-lists/ to see how operators work on Python lists. The second and third loops are the same; you just need to clear your temp_1 list in every iteration on x .

For the first for loop, you are multiplying a list with a number and appending to another list(temp_1). Multiplying a list with a number n is equivalent to repeating the elements of the list n times. Since you are appending to another list after every iteration of x, the value: x*y will get appended on a new line. When you do b[x] = temp_1, it is repeating the value of temp_1 for every iteration of x. Hence a 2D list.

The second and third cases of for loops are essentially the same because range(0, 4) will generate a list [0, 1, 2, 3] as you've done manually in case 3. When y takes on values from range(4,6), we go through each value individually and multiply by x (which takes on individual values from range(0,4)). Hence, we keep multiplying individual elements and get temp_1 = [0, 0, 4, 5, 8, 10, 12, 15]. Similar to the previous case where you used b[x] = temp_1, this value repeats for each iteration of x.

To get the result you mentioned above, you can use the method mentioned by Massoud Hosseinali . Hope this helps!

For your first for loop, y = [4,5] which is a list as a whole is repeated by x times.

>>> y=[4,5]
>>> 0*y
[]
>>> 2*y
[4, 5, 4, 5]
>>> 3*y
[4, 5, 4, 5, 4, 5]

For your second and third for loop, 'for y in [4,5]:' and 'for y in range(4,6):' are the same and so you get the same result. Obviously the result is wrong since all the values are the same for all keys in the dictionary. This is because you put 'temp_1 = []' out of for loop. PLEASE NOTE that 'b[x] = temp_1' means that b[x] and temp_1 point to the same object since Python deals with objects by passing references, so every time 'temp_1' is updated, b[0] ~ b[x] are updated to the same value. You might use built-in function id() to check the address of the objects in memory.

>>> x = {0: 'apple'}
>>> y = x
>>> y
{0: 'apple'}
>>> id(x)
55111152
>>> id(y)
55111152

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM