Changing iteration variable inside for loop in Python [duplicate] - python

This question already has answers here:
Scope of python variable in for loop
(10 answers)
Closed 9 years ago.
I am trying to do something as simple as changing the varible in which I am iterating over (i) but I am getting different behaviours in both Python and C.
In Python,
for i in range(10):
print i,
if i == 2:
i = 4;
I get 0 1 2 3 4 5 6 7 8 9, but the equivalent in C:
int i;
for (i = 0; i < 10; i++) {
printf("%d", i);
if (i == 2)
i = 4;
}
I get 01256789 (note that numbers 3 and 4 don't appear, as expected).
What's happening here?

Python has a few nice things happening in the background of for loops.
For example:
for i in range(10):
will constantly set i to be the next element in the range 0-10 no matter what.
If you want to do the equivalent in python you would do:
i = 0
while i < 10:
print(i)
if i == 2:
i = 4
else: # these line are
i += 1 # the correct way
i += 1 # note that this is wrong if you want 1,2,4,5,6,7,8,9
If you are trying to convert it to C then you have to remember that the i++ in the for loop will always add to the i.

The function range() creates a list.
For example, range(10) will create the following list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].
When you say for i in range(10), first off all the list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] will be generated, and then i will be assigned all the values from that list, in order.
It doesn't matter if you change the value of i because on the next iteration it will be assigned the next element from the list.
It C/C++ on the other hand, at the end of each iteration the value of i is incremented and then compared to the maximum allowed value (in this case 9) and if it is greater, then the loop ends.

When you call range(10) you create an iteratable list [0,1,2,3,4,5,6,7,8,9].
And the for loop just pick up one number after the other from the list at each turn, whether or not you haved changed the value of i.

Python gives you the elements in range(10), one after another. C repeatedly increments a variable.
Both of them don't really care what else you do with the variable inside the loop, but since the two language constructs do slightly different things, the outcome is different in some cases.

You can not do this by using range function.
you have to do it by using while loop only because for loop uses range function and in range function variable will get incremented by its internal method no matter what you specify in the loop it will get incremented by range list only.
for i in range(10):
... print i
... if i == 2:
... i = 4
... else:
... i += 1
...
0
1
2
3
4
5
6
7
8
9
An interesting example is here....
for i in range(10):
... print i
... i = i + 10
... print i
...
this will print...
0
10
1
11
2
12
3
13
4
14
5
15
6
16
7
17
8
18
9
19

It's because when you use the range() function in python. Your variable i will be go through the value in range. For example,
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
However, the C language that you have written is just using the normally condition to change the value of i. There is no function involved.

Related

Printing the number of different numbers in python

I would like to ask a question please regarding printing the number of different numbers in python.
for example:
Let us say that I have the following list:
X = [5, 5, 5]
Since here we have only one number, I want to build a code that can recognize that we have only one number here so the output must be:
1
The number is: 5
Let us say that I have the following list:
X = [5,4,5]
Since here we have two numbers (5 and 4), I want to the code to recognize that we have only two numbers here so the output must be:
2
The numbers are: 4, 5
Let us say that I have the following list:
X = [24,24,24,24,24,24,24,24,26,26,26,26,26,26,26,26]
Since here we have two numbers (24 and 26), I want to the code to recognize that we have only two numbers here so the output must be:
2
The numbers are: 24, 26
You could keep track of unique numbers with a set object:
X = [1,2,3,3,3]
S = set(X)
n = len(S)
print(n, S) # 3 {1,2,3}
Bear in mind sets are unordered, so you would need to convert back to a list and sort them if needed.
you can change this list into set, it will remove duplicate, then you can change it again into list.
list(set(X))
You can try numpy.unique, and use len() on the result
May I ask you please if we can use set() to read the data in a specific column in pandas?
For example, I have the following the DataFrame:
df1= [ 0 -10 2 5
1 24 5 10
2 30 3 6
3 30 2 1
4 30 4 5 ]
where the first column is the index..
I tried first to isolate the second column
[-10
24
30
30
30]
using the following: x = pd.DataFrame(df1, coulmn=[0])
Then, I transposed the column using the following XX = x.T
Then, I used set() function.
However, instead of obtaining
[-10 24 30]
I got the following [0 1 2 3 4]
So set() read the index instead of reading the first column

Default stepsize is already 1, so why does adding [::1] change my output?

I encountered something I don't understand. I created a simple example in order to explain it. I have the following list:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I iterate through the list, printing the value at the current index, and remove the value if it is even. This results in the next value getting skipped because they all move up one index (filling the gap created by the removal).
for i in numbers:
print(i)
if i % 2 == 0:
numbers.remove(i)
This results in the following output, which is as expected:
1
2
4
6
8
10
When iterating through the list backwards, this problem will be avoided, since all the lower indexes won't be affected by the removal of a higher index.
for i in numbers[::-1]:
print(i)
if i % 2 == 0:
numbers.remove(i)
Which results in (as expected):
10
9
8
7
6
5
4
3
2
1
Now we've arrived at the part I don't understand. As far as I know the default stepsize is 1, so adding [::1] shouldn't make any difference right? Well, it does...
for i in numbers[::1]:
print(i)
if i % 2 == 0:
numbers.remove(i)
Which results in:
1
2
3
4
5
6
7
8
9
10
As yous see all the numbers are getting printed, while I expected some to be skipped due to the shifting explained earlier.
Can someone explain why this is?
So, the answer is already there in the comments, just a proper explanation here:
Basically, when you say:
for i in numbers:
print(i)
if i % 2 == 0:
numbers.remove(i)
You are iterating through the list numbers
But when you write:
for i in numbers[::1]:
print(i)
if i % 2 == 0:
numbers.remove(i)
It can be seen as an equivalent to:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]# <-------------
num2 = numbers.copy() #or numbers[::1] <-------------- |
# i.e. python automatically creates a copy of the | |
# list numbers to iterate through | |
# since it seems like you are iterating through a | |
# slice of the original list | |
# | |
for i in numbers[::1]: # iterates through------------- |
print(i) # |
if i % 2 == 0: # |
numbers.remove(i) # this removes elements from --
The difference? The copy created is only stored until iteration is done. Hence even when the elements are removed, since the current list is the same, no number is skipped.

How do I go back and forth in a loop?

So I have a sequence of items at these indexes 1 2 3 4 5 6 7 8 9 10. I need to add middle two elements at first:
output = i[5]+i[6]
and for every next iteration, output must be add with left element and then right element.
output = output + i[4]
next step,
output = output + i[7]
I don't know how do that in loop or any other structure. Could anyone recommend anything to me? I'm working with python, but I just need pseudocode.
To get an element from either left or either right for each iteration I'd use a while statement in combination with a pop() on the list.
Code:
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
total = 0
while len(l):
total += l.pop(len(l) // 2)
Output:
print(total)
55
The total progression is:
0 + 6 = 6
6 + 5 = 11
11 + 7 = 18
18 + 4 = 22
22 + 8 = 30
30 + 3 = 33
33 + 9 = 42
42 + 2 = 44
44 + 10 = 54
54 + 1 = 55
newlist=[1,2,3,4,5,6,7,8,9,10]
while(len(newlist)>0):
lengthi=(len(newlist)//2)-1
lengthj=(len(newlist)//2)
sum=(newlist[lengthi]+newlist[lengthj])
del newlist[lengthi:(lengthj)+1]
print(sum)
This works for the list I have tried it, however this will change according to the length of your list. This works for even, if the length changes the code gives errors because there will be a non matched element at the end. Also you can do this by not deleting the list by copying the variable into another variable and then deleting the variables from it is probably better.
The common item here is that the two subscripts have to add up to 11.
output = 0
for left in range(5, 0, -1):
right = 11 - left
output += i[left]
output += i[right]
I left the solution longer than necessary, since you seem to have other steps in between.
Well you can start from middle of the list and then as you go forward, add left and right elements together.
m = len(a)//2 # middle of the list
output = 0
for i in range(m):
output += a[m-i-1] + a[m+i]
I'm trusting here that length of the list is an even number. Although odd length won't be a problem, just that the last element of list will be ignored.

How do I generate a random list in Python with duplicates numbers

So I just started programming in Python a few days ago. And now, im trying to make a program that generates a random list, and then, choose the duplicates elements. The problem is, I dont have duplicate numbers in my list.
This is my code:
import random
def generar_listas (numeros, rango):
lista = [random.sample(range(numeros), rango)]
print("\n", lista, sep="")
return
def texto_1 ():
texto = "Debes de establecer unos parámetros para generar dos listas aleatorias"
print(texto)
return
texto_1()
generar_listas(int(input("\nNumero maximo: ")), int(input("Longitud: ")))
And for example, I choose 20 and 20 for random.sample, it generates me a list from 0 to 20 but in random position. I want a list with random numbers and duplicated.
What you want is fairly simple. You want to generate a random list of numbers that contain some duplicates. The way to do that is easy if you use something like numpy.
Generate a list (range) of 0 to 10.
Sample randomly (with replacement) from that list.
Like this:
import numpy as np
print np.random.choice(10, 10, replace=True)
Result:
[5 4 8 7 0 8 7 3 0 0]
If you want the list to be ordered just use the builtin function "sorted(list)"
sorted([5 4 8 7 0 8 7 3 0 0])
[0 0 0 3 4 5 7 7 8 8]
If you don't want to use numpy you can use the following:
print [random.choice(range(10)) for i in range(10)]
[7, 3, 7, 4, 8, 0, 4, 0, 3, 7]
random.randrange is what you want.
>>> [random.randrange(10) for i in range(5)]
[3, 2, 2, 5, 7]

for loop conversion from C to Python

How can I write the for loop in Python as I write it in C:
for(i=0;i<10;)
{
if(i%2==0)
i=i+3;
else
i++;
printf("%d\n",i);
}
Can anyone tell me about this? I searched a lot but couldn't find it. I wrote it like this in Python:
for i in range(0,10):
if (i%2==0):
i+=3
else:
i+=1
print i
Output:
3
2
5
4
7
6
9
8
11
10
Expected output:
3
4
7
8
11
Can anyone also explain the reason of this output?
To write the same loop in Python:
i = 0
while i < 10:
if i % 2 == 0:
i += 3
else:
i += 1
print i
Which gives:
3
4
7
8
11
Note that, per the tutorial:
The for statement in Python differs a bit from what you may be used to
in C or Pascal. Rather than always iterating over an arithmetic
progression of numbers (like in Pascal), or giving the user the
ability to define both the iteration step and halting condition (as
C), Python’s for statement iterates over the items of any sequence (a
list or a string), in the order that they appear in the sequence.
In a Python for loop, any changes to the loop variable (i, in this case) that occur during the loop are ignored when the loop repeats, and the next value from the object being iterated over is used. In this case, the object is a list of numbers:
>>> range(10) # note that a 0 start is the default
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Some languages call this a for each loop. See also the language reference for more details.
range(0, 10) function returns list of values from 0 to 9:
range(0, 10) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Then for body is executed for each element in this list.
If You have 0, the 0 % 2 == 0 so it prints 0 + 3 etc.
In C You changed i value so You jumped to other value in set. Using python's for You will get through all elements. You should use
i = 0
while i < 10:
if (i % 2 == 0):
i += 3
else:
i += 1
print i
To have same results as in C
try this
for i in range(10):
if i%2 == 0:
i = i+3
else:
i = i + 1
print i
it gives the same output u asked for...hope this helps

Categories

Resources