Jack Peter 3
Jack Peter 3

Reputation: 63

How can I keep only somes values in array, list?

I have this array such as :

a = numpy.array([10,15,20,36,58]) b = [10,15,20,36,58]

And I would like to keep for a and b the values which are higher than 20 ie to get the following array/list :

c = [20,36,58] d = numpy.array([20,36,58])

Do you know how can I do this ?

Upvotes: 0

Views: 67

Answers (2)

Etienne Landuré
Etienne Landuré

Reputation: 66

This is as simple as this:

import numpy as np
a = np.array([10,15,20,36,58])
print(a[a >= 20])

Output:

[20 36 58]

Upvotes: 1

Arkistarvh Kltzuonstev
Arkistarvh Kltzuonstev

Reputation: 6920

Try this :

c = [k for k in a if k>=20]
d = numpy.array([k for k in a if k>=20])

OUTPUT :

[20, 36, 58]
array([20, 36, 58])

Upvotes: 0

Related Questions