如何解决如何从每个时间间隔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.9
。
已从每个间隔中选择元素,例如(0.0,0.2)= 0.1,(0.2,0.4)= 0.3等
第二个例子
数字为:0.3,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 Value。
应该做什么?
解决方法
我以这种方式尝试过,并且有效。
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。