- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我希望我的代码仅在满足 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.15
是 True
。通常不存在“” float 之间完全相等。)
关于python - 打印 bin 内的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24840028/
bin、bin/、bin/*和bin/**有什么区别我的 .gitignore 文件?我一直在使用 bin/,但正在查看 other .gitignore files (在 eclipse file
给定 n 个无限容量的箱子,我想将 m 件元素装入其中(每个元素都有特定的重量),同时最小化最重箱子的重量。 这不是传统的垃圾箱包装/背包问题,其中垃圾箱的容量有限,而您试图尽量减少使用的垃圾箱数量;
Closed. This question is off-topic。它当前不接受答案。 想改善这个问题吗? Update the question,所以它是用于堆栈溢出的on-topic。 7年前关
我一直在寻找一个问题的解决方案,这个问题显然比我最初想象的更不寻常。 Optaplanner 看起来很有前途,但由于我对 Java 相对缺乏经验,所以我想在深入研究之前调查一下这是否完全不可能。 我正
我有一个琐碎的问题。我有一个非常大的 df 有很多列。我正在尝试找到最有效的方法来对具有不同 bin 大小的所有列进行 bin 并创建一个新的 df。这是一个仅对单个列进行分箱的示例: import
我想以这样的方式对数字列 (var) 进行分类,使每个分类中的行数大致相同。我的附加要求是该列中的一个(唯一)值不能分配给多个 bin。例如,如果 var 列中的值 1 分配给 bin 1,则不允许将
我在 centos (rhel 7)_ 上使用 make 命令和 gcc 7.1 通过代码进行编译,但遇到了以下错误: /bin/sh:/bin: 是一个目录 which sh 返回/usr/bin/
/usr/bin/ld: cannot find -ldlib /usr/bin/ld: cannot find -lcblas /usr/bin/ld: cannot find -llapack 在
我进行了一项眼动追踪实验,试图检测两个区域(感兴趣区域,名为“代理”和“患者”的 AOI)的注视分布。我将整个时间划分为时间段,例如得到以下列表: Stimulus Participant A
我正在尝试替换 whoami带有 ~/.local/bin/ 中的脚本的命令.有没有办法让我的 whoami 获得优先权,这样当我运行 whoami 时,我的脚本就会运行? 最佳答案 这就是我的 ~/
我的项目结构是这样的: . ├── Cargo.lock ├── Cargo.toml └── src ├── bin │ └── other.rs ├── main.rs
我正在开发一个小型图书馆应用程序,它以这种格式存储技术手册: 目前,我正在尝试将库的内容保存并根据用户的需要加载到 bin 文件中。 但是,当我尝试加载库文件时,唯一的变化是“-1”被打印到控制台。此
我试图忽略 Web 项目的 bin 文件夹,但包含该 bin 文件夹中的 .refresh 文件。 这是我的 .gitignore 中的内容: [Bb]in/ #Allow .refresh file
ipv6_hdr__f_next_header_cp: coverpoint this.ipv6_hdr.ipv6_f_next_header iff (this.has_ipv6_header){
今天有人给我发了一个以 #: 开头的脚本,谷歌搜索后我没有找到任何答案。 即使脚本有效,我想知道那是什么意思。 最佳答案 哇!这让我想起了很多记忆! 回到 1980 年代和 90 年代初期,有两种基本
我使用这个 ksh 函数将“1-Jan-2011”格式转换为“1.1.2011”。 #!/bin/ksh ##---- function to convert 3 char month into nu
我在 c# winform 项目中使用 sql-server compact 数据库。 如果我查看构成解决方案的文件,我可以在以下两个文件夹中看到 exe 和 sdf 文件的副本: /bin/Debu
这个问题与我上一个问题不同。我正在通过以下代码使用 facetgrid 打印直方图。 import numpy as np import pandas as pd import seaborn as
我有一个整数需要根据概率分布分成 bin。例如,如果我有 N=100 对象进入 [0.02, 0.08, 0.16, 0.29, 0.45] 那么你可能会得到 [1, 10, 20 , 25, 44]
我在寻找使用 Jenkins 运行 Appium 的脚本时阅读了以下脚本 #!/bin/bash --login killall -9 "iPhone Simulator" &> /dev/null
我是一名优秀的程序员,十分优秀!