Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Copying specific values from one list to another, type conversion

I have a list x with values:

[0.09086322  -0.66400483  -0.85750224, ... 73.92927078, 5.18024081, -17.12200886]

Here, I want to copy values in the range (-50, 50) from list x to another list y.

I have tried implementing the following code, but it doesn’t seem to work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

y = []
for i in x:
    if x[i] >= -50 and x[i] <=50:
    y.append(x[i])

I get the following error:

only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and
integer or boolean arrays are valid indices

>Solution :

x = [0.09086322,-0.66400483,-0.85750224,73.92927078,55.18024081,-17.12200886]
y = []
for i in range(len(x)):
    if x[i]>= -50.0 and x[i] <=50.0:
        y.append(x[i])
print(y)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading