如何从每个区间 (0.0,0.2),(0.2,0.4),(0.4,0.8),(0.8,1.0) 中获取包含相等数量值的数组子集?
How can I obtain a array subset containing an equal number of values from each interval (0.0,0.2),(0.2,0.4),(0.4,0.8),(0.8,1.0)?
如何从浮点数数组 [0.0, 1.0)] 的每个区间中找到相等数量的值?
例如:
第一个例子
数字是:0.1,0.3,0.5,0.7,0.9
输出应该是:0.1,0.3,0.5,0.7,0.9
.
这里的元素是从每个区间中选择的,比如区间 (0.0,0.2) = 0.1, (0.2,0.4) = 0.3 等等
第二个例子
数字是:0.3,0.5,0.7,0.9,0.5
输出应该是None
此处不存在区间 (0.0,0.2) 的元素。因此数组子集没有过滤任何值。
我试过的代码
b = []
for item in a:
if ((item>= 0.0) and (item<= 0.2)):
b.append(item)
elif ((item>= 0.2) and (item<= 0.4)):
b.append(item)
elif ((item>= 0.4) and (item<= 0.8)):
b.append(item)
elif ((item>= 0.8) and (item<= 1.0)):
b.append(item)
但这里的问题是它不满足第二个示例的输出,如果在区间 [(0.0,0.2)] 的数组中没有元素,那么它应该打印 None 值.
应该做什么?
我试过这种方法,成功了。
import numpy as np
val = input("Enter the Numbers: ").split(',')
a = np.array(val).astype(np.float)
b = []
for item in a:
if((item >= 0.0) and (item <= 0.2)):
b.append(item)
elif ((item >= 0.2) and (item <= 0.4)):
b.append(item)
elif ((item >= 0.4) and (item <= 0.8)):
b.append(item)
elif ((item >= 0.8) and (item <= 1.0)):
b.append(item)
for item in a:
if((item >= 0.0 and item <= 0.2)) is False:
b = None
else:
break
b = np.unique(b)
print(b)
如何从浮点数数组 [0.0, 1.0)] 的每个区间中找到相等数量的值?
例如:
第一个例子
数字是:0.1,0.3,0.5,0.7,0.9
输出应该是:0.1,0.3,0.5,0.7,0.9
.
这里的元素是从每个区间中选择的,比如区间 (0.0,0.2) = 0.1, (0.2,0.4) = 0.3 等等
第二个例子
数字是:0.3,0.5,0.7,0.9,0.5
输出应该是None
此处不存在区间 (0.0,0.2) 的元素。因此数组子集没有过滤任何值。
我试过的代码
b = []
for item in a:
if ((item>= 0.0) and (item<= 0.2)):
b.append(item)
elif ((item>= 0.2) and (item<= 0.4)):
b.append(item)
elif ((item>= 0.4) and (item<= 0.8)):
b.append(item)
elif ((item>= 0.8) and (item<= 1.0)):
b.append(item)
但这里的问题是它不满足第二个示例的输出,如果在区间 [(0.0,0.2)] 的数组中没有元素,那么它应该打印 None 值.
应该做什么?
我试过这种方法,成功了。
import numpy as np
val = input("Enter the Numbers: ").split(',')
a = np.array(val).astype(np.float)
b = []
for item in a:
if((item >= 0.0) and (item <= 0.2)):
b.append(item)
elif ((item >= 0.2) and (item <= 0.4)):
b.append(item)
elif ((item >= 0.4) and (item <= 0.8)):
b.append(item)
elif ((item >= 0.8) and (item <= 1.0)):
b.append(item)
for item in a:
if((item >= 0.0 and item <= 0.2)) is False:
b = None
else:
break
b = np.unique(b)
print(b)