- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一组数据文件(例如,“data####.dat”,其中 #### = 0001,...,9999),它们都具有相同 x 的通用数据结构- 第一列中的值,然后是具有不同 y 值的多个列。
data0001.dat:
#A < comment line with unique identifier 'A'
#B1 < this is a comment line that can/should be dropped
1 11 21
2 12 22
3 13 23
data0002.dat:
#A < comment line with unique identifier 'A'
#B2 < this is a comment line that can/should be dropped
1 13 23
2 12 22
3 11 21
它们基本上源 self 的程序使用不同种子的不同运行,我现在想将这些部分结果组合成一个公共(public)直方图,以便保留以“#A”开头的注释行(所有文件都相同)和其他评论行被删除。第一列保留,然后所有其他列应该对所有数据文件进行平均:
dataComb.dat:
#A < comment line with unique identifier 'A'
1 12 22
2 12 22
3 12 22
其中 12 = (11+13)/2 = (12+12)/2 = (13+11)/2
和 22 = (21+23)/2 = (22+22)/2 = (23+21)/2
我已经有一个 bash 脚本(可能是可怕的代码;但我不是很有经验......)通过运行 ./merge.sh data* > dataComb.dat
来完成这项工作命令行。它还检查所有数据文件是否具有相同的列数和第一列中的相同值。
merge.sh:
#!/bin/bash
if [ $# -lt 2 ]; then
echo "at least two files please"
exit 1;
fi
i=1
for file in "$@"; do
cols[$i]=$(awk '
BEGIN {cols=0}
$1 !~ /^#/ {
if (cols==0) {cols=NF}
else {
if (cols!=NF) {cols=-1}
}
}
END {print cols}
' ${file})
i=$((${i}+1))
done
ncol=${cols[1]}
for i in ${cols[@]}; do
if [ $i -ne $ncol ]; then
echo "mismatch in the number of columns"
exit 1
fi
done
echo "#combined $# files"
grep "^#A" $1
paste "$@" | awk "
\$1 !~ /^#/ && NF>0 {
flag=0
x=\$1
for (c=1; c<${ncol}; c++) { y[c]=0. }
i=1
while (i <= NF) {
if (\$i==x) {
for (c=1; c<${ncol}; c++) { y[c] += \$(i+c) }
i+= ${ncol}
} else { flag=1; i=NF+1; }
}
if (flag==0) {
printf(\"%e \", x)
for (c=1; c<${ncol}; c++) { printf(\"%e \", y[c]/$#) }
printf(\"\n\")
} else { printf(\"# x -coordinate mismatch\n\") }
}"
exit 0
我的问题是,对于大量数据文件,它会很快变慢,有时会抛出“打开的文件太多”错误。我看到简单地一次性粘贴所有数据文件(paste "$@"
)是这里的问题,但分批进行并以某种方式引入临时文件似乎也不是理想的解决方案。如果能在保留脚本调用方式的同时使它更具可扩展性,我将不胜感激,即所有数据文件作为命令行参数传递
我决定也将它发布到 python 部分,因为我经常被告知处理此类问题非常方便。然而,我几乎没有使用 python 的经验,但也许这是最终开始学习它的机会 ;)
最佳答案
下面附加的代码适用于 Python 3.3 并产生所需的输出,但有一些小警告:
你可以像以前一样调用代码;例如,如果您将脚本文件命名为 merge.py,您可以执行 python merge.py data0001.dat data0002.dat
,它会将合并的平均结果打印到标准输出,就像使用 bash 脚本一样。与早期的答案之一相比,该代码还具有更大的灵 active :它的编写方式原则上应该(我还没有实际测试以确保)能够合并具有任意数量列的文件,而不仅仅是恰好有三列的文件。另一个好处是:它不会在处理完文件后保持文件打开状态; with open(name, 'r') as infile:
行是一个 Python 习惯用法,即使 close()
从未被显式调用。
#!/usr/bin/env python
import argparse
import re
# Give help description
parser = argparse.ArgumentParser(description='Merge some data files')
# Add to help description
parser.add_argument('fname', metavar='f', nargs='+',
help='Names of files to be merged')
# Parse the input arguments!
args = parser.parse_args()
argdct = vars(args)
topcomment=None
output = {}
# Loop over file names
for name in argdct['fname']:
with open(name, "r") as infile:
# Loop over lines in each file
for line in infile:
line = str(line)
# Skip comment lines, except to take note of first one that
# matches "#A"
if re.search('^#', line):
if re.search('^#A', line) != None and topcomment==None:
topcomment = line
continue
items = line.split()
# If a line matching this one has been encountered in a previous
# file, add the column values
currkey = float(items[0])
if currkey in output.keys():
for ii in range(len(output[currkey])):
output[currkey][ii] += float(items[ii+1])
# Otherwise, add a new key to the output and create the columns
else:
output[currkey] = list(map(float, items[1:]))
# Print the comment line
print(topcomment, end='')
# Get total number of files for calculating average
nfile = len(argdct['fname'])
# Sort the output keys
skey = sorted(output.keys())
# Loop through sorted keys and print each averaged column to stdout
for key in skey:
outline = str(int(key))
for item in output[key]:
outline += ' ' + str(item/nfile)
outline += '\n'
print(outline, end='')
关于python - 组合/平均多个数据文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20818522/
我在 MySQL 中有以下数据,我想求和(总计)然后除以行数。 例子: 我想对 AcctSessionTime 列中的所有数字求和并将其除以项目数,所以在我们的例子中 6+4+3+31=44 将它们除
我试图找出一个值在列中出现的平均次数,根据另一列对其进行分组,然后对其进行计算。 我有 3 张 table ,有点像这样 DVD ID | NAME 1 | 1 2 | 1 3
好吧,我完全被困在这里,如果这给你们带来任何不便,我深表歉意,但我需要你们的帮助。 我目前正在自学 C,并且从昨天开始慢慢地达到目标。所以我想给自己一个任务,让用户输入 3 个数字,程序必须找到这三个
我在使用 subAverage 类时遇到困难。当我使用 main 方法时,它似乎无法正常运行。基本上,subAverage 对数组中包含开始索引和结束索引的项进行平均。但是,当我运行它时,我得到了 3
像这样平均一个表不是问题 table = [[1,2,3,0],[1,2,3,0],[1,2,3,4]] 你可以 print numpy.average(table,axis=0) 但是如果我有不均匀
问题 -开发一个类平均脚本,每次运行时都会处理任意数量的结果。提示用户输入每个结果,直到他/她输入 -1。 (哨兵)确定类(class)平均值并将其写入页面。如果未输入结果(第一个输入为 -1),则显
我有 2 个包含以下数据的数组: Array1 = [A, A, A, A, B, B, B, C, C, C, C, C]; Array2 = [4, 2, 4, 6, 3, 9, 6, 5,
我有一个如下所示的文本文件: Mike 5 7 9 Terry 3 7 4 Ste 8 2 3 我写了下面的程序 从文本文件中检索数据 将文本分成由空格分隔的列 将每个名字后面的分数按顺序排序(最低在
我试图找到范围内数字的平均值(即找到 1-1000 范围内所有数字的平均值)。我编写了以下代码来执行此操作,但由于 if 语句,在运行时,代码会生成多个数字。然后我尝试使用 while-loop 代替
我有一系列事件。 1 是好的,0 是坏的。寻找寻找 1 个序列的最大、最小和平均长度的最 Pythonic 方式。 例如: seq ="00100000000000110100100000000011
我有一个包含类似于以下数据的表格: Group TimePoint Value 1 0 1 1 0 2
假设我有一个类 C,它具有属性 a。 从 Python 中的 C 列表中获取 a 总和的最佳方法是什么? 我已经尝试了以下代码,但我知道这不是正确的做法: for c in c_list: t
我有一个看起来像的数据: AAA_1 AAA_2 AAA_3 BBB_1 BBB_2 BBB_3 CCC 1 1 1 1 2 2
对于分色算法,我需要对 std::vector 中的颜色值 (QRgb) 进行平均。 您建议如何做?分别对 3 个分量求和然后取平均值?不然呢? 最佳答案 自 QRgb只是一个 ARGB 格式的 32
在this问题中,我要求对精度调用曲线进行澄清。 特别是,我问我们是否必须考虑一定数量的排名才能画出曲线,还是我们可以合理地选择自己。根据answer,第二个是正确的。 但是,现在我对平均精度(AP)
我想在 UDP 数据包丢失(或丢失)问题上获得其他 SO'ers 的经验。 最初我的理解是,给定直接点对点连接,其中网卡通过交叉电缆连接,网卡上有充足的缓冲区并及时处理所述缓冲区,“应该”没有数据包丢
我有一系列数据,这些数据是通过分子动力学模拟获得的,因此在时间上是连续的,并且在某种程度上是相关的。我可以将平均值计算为数据的平均值,我想估计与以这种方式计算的平均值相关的误差。 根据 this bo
我正在使用以下averageIf公式 =AVERAGEIF('Backend Data - Aerospace'!D:D, "Total",'Backend Data - Aerospace'!E:E
我想列出所有收入超过平均工资的员工。我对此有点迷茫。我需要将所有薪水加起来然后取平均,只显示收入高于平均水平的薪水。在这方面我需要很多帮助。 我的查询不起作用 SQL> select empno,
我正在运行一些音频压缩测试并尝试 Skype's Silk .在他们的测试应用程序中,我看到压缩率为 94%。这似乎很高,这是 Silk 的典型比率吗?这与其他音频压缩编解码器有可比性吗? 最佳答案
我是一名优秀的程序员,十分优秀!