Python Forum

Full Version: Iterator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
HI,

I am new at Python and learning how to code. I am stuck at solving a task given to me. i was able to solve some parts of the problem but got stuck with iterator and can not find any help online(or i didn't understand how to use iterator. I need to run the code below multiple times to create standard deviation plot. i need help on running the code and retrieving the information to be used to generate the plot.

Thank you in advance

Code:
import numpy as np
import pandas as pd
df = pd.read_csv('mosquitos_data.csv')
data = np.array(df.Response)
np.random.shuffle(data)
array_b = data[:25]
array_w = data[:18]
water = array_w.mean()
beer = array_b.mean()
a = water - beer
a
Hello, please post your code in Python code tags, you can find help here:
https://python-forum.io/misc.php?action=help&hid=25

By iterator you mean looping through the code several times? Did you try with for loop or while loop?
https://www.learnpython.org/en/Loops
Iterators are a bit more advanced concept than a loop, but here's a simple explanation of them.

You probably want just a simple loop. If you know how many points there are in the plot, you can use a for-loop. Otherwise, a while-loop with a break condition.

for instance in number-of-times:
    // do stuff

incrementer = initial value
while condition is True:
    if incrementer has reached threshold value:
        break
    //do stuff
    incrementer += 1
Thank you for your help. I was able to use the for loop.