Python Nested Loop explanation - python

I’m new to python coding and i dont understand why the nested for loop is only returning 0 1 2 for the firs Iteration.
Input:
x = 3
for i in range (x):
for j in range (x):
x = 2
print (i, '',j)
Output:
0 0
0 1
0 2
1 0
1 1
2 0
2 1

x is changed after it's passed to range to make range(3). You only see the effects of the change (i.e. range(2)) on the next loop.

The Concept behind Nested for Loops:
Let us break this problem down (I am a beginner myself!)
x = 3
for i in range (x):
Now the range function has 3 parts (start, stop, step)
start: start from this number
stop: maximum value
step: increments by this value
when we say range(x); it assumes x=3 as the stop/max value of range. The start value is by default taken as 0, and the step value is by default taken as 1. So the range we get here is [0,1,2,3) {starts from 0 and stops at 3}
So the values that 'i' can take are 0,1,and 2 because 3 is max of the range; it is not included in the values i and j can take.
So output until this point:
for i in range (x):
for j in range (x):
(printing i and j separated by a whitespace)is:
0 0
0 1
0 2
0 is printed at the start and the loop is iterated 2 more times.
(you are getting all zeroes printed first for i as the statement you have written sends an instruction to print all the values of j for one value in the outer/main for loop; because loop for j is nested inside the loop for i)
Now, x= 2 means that from this point, values 'j' can take are 0,1. Hence the second part of the output:
1 0
1 1
Similarly, the the third part of the output is:
2 0
2 1
Hence the final output you get is:
0 0
0 1
0 2
1 0
1 1
2 0
2 1

Long version
In the for statement "for target in expression :" the second part is an iterable object.
range is not a 'reserved' word; it is the name of a built-in type(class) witch is iterable
Note : The syntax highlighter cheats if it highlights range as a reserved word. It does this, because this is generally useful as range is mainly used in association with for. But this does not change what range is. However, this can mislead you.
As a result of the above:
the correct typing is range(x) not range (x)
range(x) build an objet of type range and initialize it with x.
Short answer
x is interpreted when it is passed to range().
Code to print the range objects:
x = 3
range_i = range(x)
print(f"i loop x: {x}, range(x):{range_i}")
for i in range_i:
range_j = range(x)
print(f"j loop x: {x}, range(x):{range_j}")
for j in range_j:
x = 2
print(i, ' ',j)
Output
i loop x: 3, range(x):range(0, 3)
j loop x: 3, range(x):range(0, 3)
0 0
0 1
0 2
j loop x: 2, range(x):range(0, 2)
1 0
1 1
j loop x: 2, range(x):range(0, 2)
2 0
2 1
Rule of thumb
Unless you really know what you are doing, do not mess with the expression of the for statement.
lst = ['a', 'b', 'c', 'd']
for x in lst:
if x == 'b':
lst.remove('a')
print(x, end = ' ')
gives
a b d
And
lst = ['a', 'b', 'c', 'd']
for x in lst:
if x == 'd':
lst.insert(0, 'z')
print(x, end = ' ')
does not end and 'z' never appears:
a b c d d d d d d d d d d d d d ...
Musing around
Note : what follows is NOT recommended
You can redefine range. (The example below is a very simplified redefinition: it does not take in account the second version of range : range(start, stop[, step]) neither it cares for other range specifications)
def range(n):
i = 0
while i <= n: # note the use of '<=' instead of '<'
yield i
i += 1
And now 'range' does not behave as it should.
Example:
for i in range(3):
print(x, end = ' ')
gives:
0 1 2 3
Yes: 0..3 not 0..2 as the 'true' range.

Related

How to reduce the nested for Loop complexity to a single loop in python?

for i in range(0,x):
for j in range(0,y):
if (i+j)%2 == 0:
Think of something like tossing two dices at the same time and finding if the sum on the dices is an even number but here's the catch, a dice has 6 sides but here the two can have any number of sizes, equal and not equal even!
Can anyone suggest how to merge it under one loop because I can't think of any?
based on Python combine two for loops, you can merge two for loops in a single line by importing itertools as below:
import itertools
for i, j in itertools.product(range(0,x), range(0,y)):
if (i+j)%2 == 0:
You can't get rid of the nested loop (you could hide it, like by using itertool.product, but it would still be executed somewhere, and the complexity would still be O(x * y)) but you can get rid of the condition, if you only need to generate the values of j that satisfy it, by adapting the range for j.
This way, you'll have about twice as less loops by avoiding the useless ones.
for i in range(0,x):
for j in range(i%2,y, 2):
print(i, j, i+j)
Output:
0 0 0
0 2 2
1 1 2
1 3 4
2 0 2
2 2 4
For me its much cleaner to leave it as two loops. Its much more readable and easier to understand whats happening. However you could essentially do x * y then use divmod to calculate i and j
x = 2
y = 3
for i in range(0,x):
for j in range(0,y):
print(i, j, i+j)
print("###")
for r in range(x*y):
i, j = divmod(r, y)
print(i, j, i + j)
OUTPUT
0 0 0
0 1 1
0 2 2
1 0 1
1 1 2
1 2 3
###
0 0 0
0 1 1
0 2 2
1 0 1
1 1 2
1 2 3

Python nested loop miscounting instances of integers in list

I'm banging my head against the wall trying to figure out why this nested loop is miscounting the number of times an integer occurs in a list. I've set up a function to take two lines of input, n and ar, where n is the number of integers in ar and ar is the list of integers. My code is below:
import sys
n = sys.stdin.readline()
n = int(n)
ar = sys.stdin.readline()
ar = ar.split(' ')
ar = [int(i) for i in ar]
def find_mode(n,ar):
# create an empty dict and initially set count of all integers to 1
d = {}
for i in range(n):
d[ar[i]] = 1
for i in range(n):
# hold integer i constant and check subsequent integers against it
# increase count if match
x = ar[i]
for k in range(i+1,n):
if ar[k] == x:
d[ar[k]] += 1
print(d)
The counter seems to be increasing the count by 1 every time, which leads me to believe it's a problem with the nested loop.
>>> 9
>>> 1 2 3 4 4 9 9 0 0
{0: 2, 1: 1, 2: 1, 3: 1, 4: 2, 9: 2}
OK
>>> 10
>>> 1 2 3 4 4 9 9 0 0 0
{0: 4, 1: 1, 2: 1, 3: 1, 4: 2, 9: 2}
Count of 0 increased by +2
>>> 11
>>> 1 2 3 4 4 9 9 0 0 0 0
{0: 7, 1: 1, 2: 1, 3: 1, 4: 2, 9: 2}
Count of 0 increased by +3
I understand there might be more efficient or "pythonic" ways to count the amount of times a number occurs in a list but this was the solution I came up with and as someone still learning Python, it would help to understand why this exact solution is failing. Many thanks in advance.
This is because for each distinct number in the list (call it x) you count the number of subsequent appearances. This is fine if a number only occurs twice but if it occurs multiple times you will over-count for each additional appearance.
For example: [0, 0, 0, 0]. You iterate over the list and then for each item you iterate over the list that follows that item. So for the first 0 you count a total of 3 subsequent 0s. For the second however you will count a total of 2 and for the third a total of 1 which makes 6. This is why you have 3 too much in the end.
You can achieve this task by using collections.Counter:
>>> from collections import Counter
>>> d = Counter(ar)
I'm not exactly sure that I can fix your specific problem, but would something like this work instead?
d={}
for x in ar:
d[x] = d.get(x, 0) + 1
I understand that you want to fix your existing work as a learning exercise, but I'm not sure that that approach is even the right one. As it is, I can't really tell what you're going for, so it's hard for me to offer specific advice. I would recommend that you don't throw good time after bad.
python has a method to do exactly what you're describing.
It's called .count().
If you do ar.count(3), it will return the number of occurences of 3 in the list ar.
** In your case:**
There's no need for a nested loop as you only need one loop.
Try this:
dic = {}
for num in ar:
if num not in dic:
dic[num] = 1
else:
dic[num] += 1
This would produce the dict you want with the numbers and their occurences
You can refer to other answers as to how you should solve this problem more efficiently, but to answer the question you're asking (Why doesn't this nested loop work?):
To visualize what your nested loop is doing consider the following input:
0 0 0 0 0
Your algorithm will count the following:
0 0 0 0 0
^ ^ ^ ^ ^ (5)
then,
0 0 0 0 0
^ ^ ^ ^ (4)
then,
0 0 0 0 0
^ ^ ^ (3)
then,
0 0 0 0 0
^ ^ (2)
and finally,
0 0 0 0 0
^ (1)
What happens is it counts the number of 0's multiple times over. In this instance it will count
15 0's (5+4+3+2+1)
itertools are your friend
from itertools import groupby
def group_by_kv_l(n):
l = []
for k, v in groupby(n):
l.append((len(list(v)),int(k)))
return l
def group_by_kv_d(n):
d = {}
for k, v in groupby(n):
d[(int(k))] = len(list(v))
return d
if __name__ == "__main__":
n = input().split()
n = "".join(n)
print(
"STDIN: {}".format(n)
)
print(
group_by_kv_l(n)
)
print(
group_by_kv_d(n)
)

Nested for loop over range()

For the following code:
x = range(10)
for i in x:
print(i)
for j in x:
print(">> %d" % j)
break
I would have expected the output to be:
0
>> 1
2
>> 3
..
but instead, it is:
0
>> 0
1
>> 0
2
>> 0
..
Why does range behave in this way?
By converting x to an iterator, you can achieve your expected behavior.
x = iter(range(10))
for i in x:
print(i)
for j in x:
print('> {}'.format(j))
break
which returns
0
> 1
2
> 3
4
> 5
6
> 7
8
> 9
What this shows us is that the problem does not lie with the breaking out of the inner loop per say but rather with range not being depleted as you loop over it. This happens because range is not an iterator and thus it restarts every time instead of picking up from where it left off.
for j in x:
print(">> %d" % j)
break
You are breaking the loop of J causing the value of J to reset and go back to 0 everytime.

A loop for adding list element without high memory performance?

Everybody,
a =[0, 0, 2, 4, 6]
x=5
This a list (a) and a fix value (x).
I need a loop codes which must add with x every element of list and add this value with previous list elements in every loop ( loop must continue as x value). Other words result should like below:
0
0
2
4
6
0
0
7
9
11
0
0
12
14
16
0
0
15
19
21
0
0
21
24
26
I prepared codes as below but it doesn’t work. Other words produce something as below (incorrect)
i=0
counter=0
while counter < x:
for i in a:
if i >0:
i=i+x
elif i ==0:
i=0
print i
counter=counter+1
0
0
7
9
11
0
0
7
9
11
0
0
7
9
11
0
0
7
9
11
0
0
7
9
11
So, I need to help for this…
Thank you.
I think this does mostly what you want (at least, as I understand the question)...
def make_it_so(a, x):
i = 0
counter=0
while counter < x:
for i in a:
if i == 0:
yield 0
else:
yield i + counter * x
counter = counter + 1
# Demo
for item in make_it_so([0, 0, 2, 4, 6], 5):
print item
Note that I've made it a generator function. You could easily turn it into a regular function that returns a list if you created an output list at the top of the function and swapped yield ... for output_list.append(...) and then return output_list at the end of the function...
The key here is to understand that in the first loop, you are adding 0 to all of the (non-zero) items. In the second loop, you are adding x. In the third loop, you're adding the x + x (since the first loop added x and now you're adding x more). In general, for the Nth loop, you'll be adding (N-1) * x to all of the non-zero items. So, you just need to keep track of N, (or N-1). In fact, your original code was already doing this (with counter), so we just re-purpose that and it's all good.
You need to change the values in a, not just add to the numbers you get out of a (because you'll keep getting the same ones out). Also, you need to print out the original values.
def process(x, memo):
return [n+x if n else n for n in memo]
res = a
memo = a
for _ in range(x - 1):
memo = process(x, memo)
res.extend(memo)

Using `for` in `range(x)` loop

Shouldn't both blocks of code print similar results? Why is the range function inside of the inner loop reevaluated each time the inner for statement is reached while the range function in the outerloop is only evaluated once?
x = 4
for j in range(x)
for i in range(x)
print i
x = 2
Results
0
1
2
3
0
1
0
1
0
1
I know the first 4 integers printed ( 0 - 3) are a result of the code
for j in range(x): code but why are the the following also printed?
0
1
0
1
0
1
The code
x = 4
for j in range(x):
print i
x = 5
Prints
0 1 2 3
Additional Info
Python 2.7 in IDLE
I can only explain by walking through the iterations of the loops, so here goes:
x = 4
for j in range(x)
for i in range(x)
print i
x = 2
First time through.
x = 4
for j in [0, 1, 2, 3]
for i in range [0, 1, 2, 3]
print i
x = 2
prints
0
1
2
3
Now x is set as 2, but the outer loops range has already been executed, so it is not reevaluated.
Code now becomes:
for j in [0, 1, 2, 3]:
for i in [0, 1]:
print i
x = 2
prints
0
1
And this continues two more times.
Function range(x) produces a list of [0,1,2,3,4]. In for loop you iterate over this list.
Your code is equivalent to:
for j in [0,1,2,3]:
for i in [0,1,2,3]:
print i
for i in [0,1]:
print i
for i in [0,1]:
print i
for i in [0,1]:
print i
range(x) is evaluated only once i.e. when the loop begins, that is why modifying x inside the loop has no effect.
However, in the first code clock, you change x to 2 in the inner, so the next time the inner loop is executed, range only gives (0,1).
Edit: your first block of code is equivalent to
x = 5
for i in range(x)
print i
x = 2
Here is how I view it:
Inner loop is executed for each iteration of outer loop
x = 4
j = 0
for i in range (x):
print(i)
x = 2
j = 1
for i in range(x) # this x is no longer 4 but it is 2
print(i)
x = 2
j = 2
for i in range (x): # this x is, of course, 2
print(i)
x = 2
j = 3
for i in range (x): # this x is, of course, 2
print(i)
x = 2
j is not printed, insert " print('xx', j, 'oo') " before " for i in range(x): "
then change to "print('xx', x, 'oo')
or change 'for j in range(x):' to 'for j in range(225,200,-5):' follow with 'print(j)' before 'for i in range(x):'

Categories