0

I have a long list of large floating point numbers:

lis = [0.10593584063824,... ,9.5068763787043226e-34, 9.8763787043226e-34, 8.3617950149494853e-34]

How can I pick into a new array all the numbers that fulfill certain criteria? For example, how can I pick up into a new list all the numbers that are a_constant > .95

AGN Gazer
  • 8,025
  • 2
  • 27
  • 45
J.Do
  • 428
  • 1
  • 5
  • 18

3 Answers3

1

You could use a list comprehension :

lis = [0.10593584063824,... ,9.5068763787043226e-34, 9.8763787043226e-34, 8.3617950149494853e-34]
out = [x for x in lis if 0.65 > x > 0.95]
Loïc
  • 11,804
  • 1
  • 31
  • 49
1

Using np.where:

>>> import numpy as np

>>> l = [0.2, 0.4, 0.5]

# Case list into numpy array type.
>>> l_arr = np.array(l)

# Returns the indices that meets the condition.
>>> np.where(l_arr > 0.3)
(array([1, 2]),)

# Returns the values that meets the condition.
>>> l_arr[np.where(l_arr > 0.3)]
array([ 0.4,  0.5])
alvas
  • 115,346
  • 109
  • 446
  • 738
1

I would use numpy- if you are using this in a data science job.First,convert the list into a numpy array, then apply the condition and convert the numpy array back to list.

import numpy as np

lis = [0.10593584063824e-34,2.5068763787043226e-34,9.5068763787043226e-34, 9.8763787043226e-34, 8.3617950149494853e-34]

#Convert the list into a numpy array
np_array = np.array([[lis]])

#filter the np array and convert back to a list
new_lis = (np_array[np_array > 3.7e-34]).tolist()

[9.506876378704323e-34, 9.8763787043226e-34, 8.361795014949486e-34]
Vijay
  • 91
  • 9