Index of the current position inside of a python loop with increments?
Question:
I’m trying to write an array of values within two nested loops over another ndarray with at the corresponding position. Enumerate does not work, because of the increments.
My example-Code does the job, but I’m fairly certain, there are more elegant ways:
basearray = np.array([[10, 20, 30, 40, 50],
[15, 16, 17, 18, 19],
[25, 26, 27, 28, 29],
[35, 36, 37, 38, 39]])
stepx=0
stepy=0
incrementx=2
incrementy=2
resultarray=np.zeros(((len(basearray)),len(basearray[1])))
for x in basearray[::incrementx]:
stepy=0
for y in x[::incrementy]:
resultarray[stepx][stepy]=y*2
stepy=stepy+incrementy
stepx=stepx+incrementx
print(resultarray)
[[ 20. 0. 60. 0. 100.]
[ 0. 0. 0. 0. 0.]
[ 50. 0. 54. 0. 58.]
[ 0. 0. 0. 0. 0.]]
How can I solve that?
Very best and thank you in advance
Christian
Answers:
If I understood correctly, one approach is to do:
import numpy as np
basearray = np.array([[10, 20, 30, 40, 50],
[15, 16, 17, 18, 19],
[25, 26, 27, 28, 29],
[35, 36, 37, 38, 39]])
resulting_array = np.zeros((4, 5))
resulting_array[::2, ::2] = basearray[::2, ::2]
print(resulting_array)
Output
[[10. 0. 30. 0. 50.]
[ 0. 0. 0. 0. 0.]
[25. 0. 27. 0. 29.]
[ 0. 0. 0. 0. 0.]]
If you are intialising zeroes in the form of list the desired output can be achievable
Code:-
import numpy as np
basearray=np.array([[10, 20, 30, 40, 50],
[15, 16, 17, 18, 19],
[25, 26, 27, 28, 29],
[35, 36, 37, 38, 39]])
resulting_array1=([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
for lis in range(0,len(basearray)-1,2):
for y in range(0,len(basearray[0]),2):
resulting_array1[lis][y]='result'
print(resulting_array1)
Output:-
[['result', 0, 'result', 0, 'result'], [0, 0, 0, 0, 0], ['result', 0, 'result', 0, 'result'], [0, 0, 0, 0, 0]]
Points to remember:-
(1) The elements of a NumPy array must all be of the same type. you can not change integer type to string type result
Code:-2 Using np array
import numpy as np
basearray=np.array([[10, 20, 30, 40, 50],
[15, 16, 17, 18, 19],
[25, 26, 27, 28, 29],
[35, 36, 37, 38, 39]])
resulting_array2=np.array([['0 ', '0', '0 ', '0', '0 '],
['0', '0', '0', '0', '0'],
['0 ', '0', '0 ', '0', '0 '],
['0', '0', '0', '0', '0']])
for lis in range(0,len(basearray)-1,2):
for y in range(0,len(basearray[0]),2):
resulting_array2[lis][y]='result'
print(resulting_array2)
Output:-
[['result' '0' 'result' '0' 'result']
['0' '0' '0' '0' '0']
['result' '0' 'result' '0' 'result']
['0' '0' '0' '0' '0']]
Note you have to give spaces in 0 when initialising..!
i.e if you give two spaces with 0 it will return only res
so to return result you have to write 0 with 4 spaces to print result
I’m trying to write an array of values within two nested loops over another ndarray with at the corresponding position. Enumerate does not work, because of the increments.
My example-Code does the job, but I’m fairly certain, there are more elegant ways:
basearray = np.array([[10, 20, 30, 40, 50],
[15, 16, 17, 18, 19],
[25, 26, 27, 28, 29],
[35, 36, 37, 38, 39]])
stepx=0
stepy=0
incrementx=2
incrementy=2
resultarray=np.zeros(((len(basearray)),len(basearray[1])))
for x in basearray[::incrementx]:
stepy=0
for y in x[::incrementy]:
resultarray[stepx][stepy]=y*2
stepy=stepy+incrementy
stepx=stepx+incrementx
print(resultarray)
[[ 20. 0. 60. 0. 100.]
[ 0. 0. 0. 0. 0.]
[ 50. 0. 54. 0. 58.]
[ 0. 0. 0. 0. 0.]]
How can I solve that?
Very best and thank you in advance
Christian
If I understood correctly, one approach is to do:
import numpy as np
basearray = np.array([[10, 20, 30, 40, 50],
[15, 16, 17, 18, 19],
[25, 26, 27, 28, 29],
[35, 36, 37, 38, 39]])
resulting_array = np.zeros((4, 5))
resulting_array[::2, ::2] = basearray[::2, ::2]
print(resulting_array)
Output
[[10. 0. 30. 0. 50.]
[ 0. 0. 0. 0. 0.]
[25. 0. 27. 0. 29.]
[ 0. 0. 0. 0. 0.]]
If you are intialising zeroes in the form of list the desired output can be achievable
Code:-
import numpy as np
basearray=np.array([[10, 20, 30, 40, 50],
[15, 16, 17, 18, 19],
[25, 26, 27, 28, 29],
[35, 36, 37, 38, 39]])
resulting_array1=([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
for lis in range(0,len(basearray)-1,2):
for y in range(0,len(basearray[0]),2):
resulting_array1[lis][y]='result'
print(resulting_array1)
Output:-
[['result', 0, 'result', 0, 'result'], [0, 0, 0, 0, 0], ['result', 0, 'result', 0, 'result'], [0, 0, 0, 0, 0]]
Points to remember:-
(1) The elements of a NumPy array must all be of the same type. you can not change integer type to string type result
Code:-2 Using np array
import numpy as np
basearray=np.array([[10, 20, 30, 40, 50],
[15, 16, 17, 18, 19],
[25, 26, 27, 28, 29],
[35, 36, 37, 38, 39]])
resulting_array2=np.array([['0 ', '0', '0 ', '0', '0 '],
['0', '0', '0', '0', '0'],
['0 ', '0', '0 ', '0', '0 '],
['0', '0', '0', '0', '0']])
for lis in range(0,len(basearray)-1,2):
for y in range(0,len(basearray[0]),2):
resulting_array2[lis][y]='result'
print(resulting_array2)
Output:-
[['result' '0' 'result' '0' 'result']
['0' '0' '0' '0' '0']
['result' '0' 'result' '0' 'result']
['0' '0' '0' '0' '0']]
Note you have to give spaces in 0 when initialising..!
i.e if you give two spaces with 0 it will return only res
so to return result you have to write 0 with 4 spaces to print result