gpt4 book ai didi

python - 打印 bin 内的元素

转载 作者:太空宇宙 更新时间:2023-11-03 18:17:09 25 4
gpt4 key购买 nike

我希望我的代码仅在满足 if 条件时才打印,然后清空数组以打印下一个结果

import numpy as np
r = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.000570290882095, 0.0107443912719, 0.0124509177244,
0.0125, 0.0125, 0.025, 0.025, 0.025,
0.025, 0.0256640229497, 0.030566379892, 0.031401430789,
0.0375, 0.0375, 0.0375, 0.0395298596851, 0.039780154486,
0.0438643740073, 0.0466295394557, 0.0480063782397, 0.05,
0.05, 0.068990534098, 0.0717060855612, 0.0737078626994,
0.0783505963591, 0.0875, 0.0875, 0.100794816139,
0.110492949738, 0.1125, 0.125, 0.125, 0.137197807346,
0.140625, 0.147676814534, 0.149311786297, 0.15, 0.15,
0.153789751195, 0.15653721735, 0.161158308383,
0.165614224138,0.165804856115, 0.181477147577, 0.186858748434, 0.2]
for binStartLoad in np.arange(0, 1, 0.05):
k = []
for i in range(len(r)):
if binStartLoad >= r[i] < binStartLoad + 0.05:
k.append(r[i])
print k

现在我的结果是:

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.000570290882095, 0.0107443912719, 0.0124509177244, 0.0125, 0.0125,
0.025, 0.025, 0.025, 0.025, 0.0256640229497, 0.030566379892,
0.031401430789, 0.0375, 0.0375, 0.0375, 0.0395298596851,
0.039780154486, 0.0438643740073, 0.0466295394557,
0.0480063782397, 0.05, 0.05]
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.000570290882095, 0.0107443912719, 0.0124509177244, 0.0125,
0.0125, 0.025, 0.025, 0.025, 0.025, 0.0256640229497,
0.030566379892, 0.031401430789, 0.0375, 0.0375, 0.0375,
0.0395298596851,0.039780154486, 0.0438643740073, 0.0466295394557,
0.0480063782397, 0.05, 0.05, 0.068990534098, 0.0717060855612,
0.0737078626994, 0.0783505963591, 0.0875, 0.0875] ans so on

但我希望我的结果是:

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
0.000570290882095, 0.0107443912719, 0.0124509177244, 0.0125, 0.0125,
0.025, 0.025, 0.025, 0.025, 0.0256640229497, 0.030566379892,
0.031401430789, 0.0375, 0.0375, 0.0375, 0.0395298596851,
0.039780154486, 0.0438643740073, 0.0466295394557,
0.0480063782397]
[0.05, 0.05, 0.068990534098, 0.0717060855612, 0.0737078626994,
0.0783505963591, 0.0875, 0.0875]
[0.100794816139, 0.110492949738, 0.1125, 0.125, 0.125, 0.137197807346,
0.140625, 0.147676814534, 0.149311786297] and so on

最佳答案

您似乎想要的是将 r 拆分为宽度为 0.05 的容器。一种更 NumPyish 的方法可能是搜索断点索引:

# find the indices for the break points (0, 0.05, 0.10, .., 1.00)
bpoints = np.searchsorted(r, np.arange(0, 1+.01, 0.05), side='left')

# now the values 0.05 are at indices bpoints[0]..bpoints[1], etc.
# the resulting vectors are collected into `bins`
bins = [ r[bpoints[i]:bpoints[i+1]] for i in range(len(bpoints)-1) ]

那么bins是:

[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.000570290882095, 0.0107443912719, 0.0124509177244, 0.0125, 0.0125, 0.025, 0.025, 0.025, 0.025, 0.0256640229497, 0.030566379892, 0.031401430789, 0.0375, 0.0375, 0.0375, 0.0395298596851, 0.039780154486, 0.0438643740073, 0.0466295394557, 0.0480063782397], 
[0.05, 0.05, 0.068990534098, 0.0717060855612, 0.0737078626994, 0.0783505963591, 0.0875, 0.0875],
[0.100794816139, 0.110492949738, 0.1125, 0.125, 0.125, 0.137197807346, 0.140625, 0.147676814534, 0.149311786297, 0.15, 0.15],
[0.153789751195, 0.15653721735, 0.161158308383, 0.165614224138, 0.165804856115, 0.181477147577, 0.186858748434],
[0.2],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]]

空列表表示没有值的箱。当然,在打印阶段很容易摆脱这些:

print "\n".join([ str(b) for b in bins if len(b) > 0 ])

但是,通常需要保留空垃圾箱以避免困惑索引。

请注意,由于浮点舍入误差而导致边界情况(例如 0.15)最终出现的情况是很难预测的。 (这里 0.15 最终出现在错误的位置,因为 np.arange(0,1.01,0.05)[3] > 0.15True。通常不存在“” float 之间完全相等。)

关于python - 打印 bin 内的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24840028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com