我有一长串大浮点数:
lis = [0.10593584063824,... ,9.5068763787043226e-34, 9.8763787043226e-34, 8.3617950149494853e-34]
如何选择符合特定条件的所有数字?例如,如何将所有a_constant > .95
答案 0 :(得分:1)
您可以使用列表理解:
lis = [0.10593584063824,... ,9.5068763787043226e-34, 9.8763787043226e-34, 8.3617950149494853e-34]
out = [x for x in lis if 0.65 > x > 0.95]
答案 1 :(得分:1)
使用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])
答案 2 :(得分:1)
如果你在数据科学工作中使用它,我会使用numpy。首先,将列表转换为numpy数组,然后应用条件并将numpy数组转换回列表。
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]