#data-science-and-ml
1 messages · Page 423 of 1
myArr = np.zeros(0)
hana= [1,34,45,64,342,57,345,234]
for i in range(0,4):
myArr=np.append(hana,hana[i])
print(myArr)
i only want to add 1,34,45,64 to the array myArr but when i run this why does it adds all the value to the myArr array . Somebody help plze.
the problem is in the loop. you're replacing myArr with a concatenation of hana with an item in hana, and in the next iteration you delete this result and compute a new concatenation of hana with a different item
can you give the correct syntax please
.
based on your code,
import numpy as np
myArr = np.zeros(0)
hana= [1,34,45,64,342,57,345,234]
for i in range(0,4):
myArr=np.append(myArr,hana[i])
print(myArr)
but honestly this is not what you want to do. you'd really do this instead
import numpy as np
hana= [1,34,45,64,342,57,345,234]
myArr = hana[0:4]
with slice notation
oops i forgot to make the change in the first block of code. i fixed it now
actually in my case i only want specific values of i from a loop
i'll give you that part of code as well just a sec
for i in range (0,688128):
if flat_gt[i]==255:
W_val_hs= np.append(flat_hs,[flat_hs[i]])
that has the same mistake
so in this case how do i take only a specific set of value of i ?
here slicing won't work right so how do i make sure i get only specific value and not all
i'd do something like ```py
In [7]: W_val_hs = np.array([0, 100, 255, 255, 3, 255])
In [8]: hana = np.array([ 300, 12039, 43, 93, 123, 34 ])
In [9]: hana[ W_val_hs == 255 ]
Out[9]: array([43, 93, 34])
which is more general than slicing. so-called "fancy indexing", which is great with numpy because operations easily broadcast over arrays
pip install