There's various routes that you can take. Assuming you want to print the first 3 elements of your array, here's two:
array=[1,2,3,4,5]
1) Slice the array
# start at index 0 and go until index 2
for record in array[0:3]:
print(record)
2) Use a variable to iterate over the array
# range(3) = [0, 1, 2]
for i in range(3):
print(array[i])
Answer from randyjp on Stack OverflowThere's various routes that you can take. Assuming you want to print the first 3 elements of your array, here's two:
array=[1,2,3,4,5]
1) Slice the array
# start at index 0 and go until index 2
for record in array[0:3]:
print(record)
2) Use a variable to iterate over the array
# range(3) = [0, 1, 2]
for i in range(3):
print(array[i])
In this code part-
array=[1,2,3,4,5]
for record in range(0,2,array):
print(record)
The range function takes 3 parameters. First one is starting index,second one is ending index and the third one is the gap in which you want the numbers to be. So,going by an example-
for i in range(0,5,2): print(i)
This code will print 0,2,4 as answer. The range function will start from 0 and will return every 2nd number till 4(not 5,because 5 is excluded).
Your code gives an error because you have mentioned an array as your 3rd parameter but it is expecting an integer
for record in range(0,2,len(array)):
print(record)
You could try this,but it will only return 0 because essentially this for loop simplifies to-
for record in range(0,2,5)
So after returning the starting number it will try to get the next 5th number which you dont have(as 2nd parameter is 2). Hope this clarifies.
Using range() in accessing elements of an array
python - Finding range of a numpy array elements - Stack Overflow
[Python] How you would get a range when working with arrays.
python - How do I create a list with numbers between two values? - Stack Overflow
What is the difference between NumPy arange and linspace?
What is NumPy arange?
What is NumPy linspace?
import numpy as np outcomes1000 = np.random.choice(die, size = 1000, p = probabilities) averages = [ np.mean(outcomes1000[range(0,i)]) for i in range(1,1001)]
What does outcomes1000[range(0,i)] do? Can we use range() to access elements of an array?
I am taking a coding class with python for one of my grad school classes (Bio background). We are working in Spyder3
import numpy
x = numpy.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print(x[0,2])
r=x.shape
for num in range(1,r[1]):
print(x[num-1,:])
print("Done!")The above is the line of code my professor gave us in class. For the life of my I can't figure out where the (1,r[1]) comes from, I understand that r is referring back to what the variable "r" stands for but why are we using 1 to r[1] for the range? I know it stands for column and row of the matrix, the matrix is a 3,4. I hope if this is enough information, if not I will update!
Use range. In Python 2, it returns a list directly:
>>> range(11, 17)
[11, 12, 13, 14, 15, 16]
In Python 3, range is an iterator. To convert it to a list:
>>> list(range(11, 17))
[11, 12, 13, 14, 15, 16]
Note: The second number in range(start, stop) is exclusive. So, stop = 16+1 = 17.
To increment by steps of 0.5, consider using numpy's arange() and .tolist():
>>> import numpy as np
>>> np.arange(11, 17, 0.5).tolist()
[11.0, 11.5, 12.0, 12.5, 13.0, 13.5,
14.0, 14.5, 15.0, 15.5, 16.0, 16.5]
See: How do I use a decimal step value for range()?
You seem to be looking for range():
>>> x1=11
>>> x2=16
>>> range(x1, x2+1)
[11, 12, 13, 14, 15, 16]
>>> list1 = range(x1, x2+1)
>>> list1
[11, 12, 13, 14, 15, 16]
For incrementing by 0.5 instead of 1, say:
>>> list2 = [x*0.5 for x in range(2*x1, 2*x2+1)]
>>> list2
[11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0]