[英]Difference of consecutive float numbers in a column
I have a list of floating point numbers in a file in column like this: 我在像这样的列中的文件中有一个浮点数列表:
123.456 123.456
234.567 234.567
345.678 345.678
How can i generate an output file which is generated by subtracting the value in a line with the value just above it. 我如何生成输出文件,该输出文件是通过将一行中的值减去刚好在其上方的值而生成的。 For the input file above,the output generated should be:
对于上面的输入文件,生成的输出应为:
123.456-123.456 123.456-123.456
234.567-123.456 234.567-123.456
345.678-234.567 345.678-234.567
The first value should return zero, but the other values should get subtracted with the value just above it. 第一个值应返回零,而其他值应减去正上方的值。 This is not an homework question.
这不是一个作业问题。 This is a small requirement of my bigger problem and i am stuck at this point.
这是我更大的问题的一个很小的要求,而我现在被困在这里。 Help much appreciated.
帮助非常感谢。 Thanks !!
谢谢 !!
This will work: 这将起作用:
diffs = [0] + [j - data[i] for i,j in enumerate(data[1:])]
So, assuming data.txt
contains: 因此,假设
data.txt
包含:
123.456
234.567
345.678
then 然后
with open('data.txt') as f:
data = f.readlines()
diffs = [0] + [float(j) - float(data[i]) for i,j in enumerate(data[1:])]
print diffs
will yield 将产生
[0, 111.111, 111.11099999999999]
This answer assumes you want to keep the computed values for further processing. 该答案假定您要保留计算值以进行进一步处理。
If at some point you want to write these out to a file, line by line: 如果您想将它们写到文件中,请逐行:
with open('result.txt', 'w') as outf:
for i in diffs:
outf.write('{0:12.5f}\n'.format(i))
and adjust the field widths to suit your needs (right now 12 spaces reserved, 5 after the decimal point), written out to file result.txt
. 并调整字段宽度以适合您的需要(现在保留12个空格,小数点后5个空格),并写到文件
result.txt
。
UPDATE : Given (from the comments below) that there is possibly too much data to hold in memory, this solution should work. 更新 :鉴于(从下面的评论)可能有太多的数据要保存在内存中,此解决方案应该工作。 Python 2.6 doesn't allow opening both files in the same
with
, hence the separate statements. Python 2.6中不允许在同一个打开这两个文件
with
,因此单独的语句。
with open('result2.txt', 'w') as outf:
outf.write('{0:12.5f}\n'.format(0.0))
prev_item = 0;
with open('data.txt') as inf:
for i, item in enumerate(inf):
item = float(item.strip())
val = item - prev_item
if i > 0:
outf.write('{0:12.5f}\n'.format(val))
prev_item = item
Has a bit of a feel of a hack. 有一点骇客的感觉。 Doesn't create a huge list in memory though.
但是不会在内存中创建巨大的列表。
给定值列表:
[values[i] - values[i-1] if i > 0 else 0.0 for i in range(len(values))]
Instead of list comprehensions or generator expressions, why not write your own generator that can have arbitrarily complex logic and easily operate on enormous data sets? 除了编写列表推导式表达式或生成器表达式之外,为什么不编写自己的生成器,它可以具有任意复杂的逻辑并可以轻松地对庞大的数据集进行操作?
from itertools import imap
def differences(values):
yield 0 # The initial 0 you wanted
iterator = imap(float, values)
last = iterator.next()
for value in iterator:
yield value - last
last = value
with open('data.txt') as f:
data = f.readlines()
with open('outfile.txt', 'w') as f:
for value in differences(data):
f.write('%s\n' % value)
If data
holds just a few values, the benefit wouldn't necessarily be so clear (although the explicitness of the code itself might be nice next year when you have to come back and maintain it). 如果
data
只包含几个值,那么好处不一定就这么明显(尽管明年代码本身的显式性在您必须返回并对其进行维护时可能会很好)。 But suppose data
was a stream of values from a huge (or infinite!) source and you wanted to process the first thousand values from it: 但是,假设
data
是来自巨大(或无限!)源的值流,并且您想处理其中的前1000个值:
diffs = differences(enormousdataset)
for count in xrange(1000):
print diffs.next()
Finally, this plays well with data sources that aren't indexable. 最后,这在不可索引的数据源中也很好。 Solutions that track index numbers to look up values don't play well with the output of generators.
跟踪索引号以查找值的解决方案在生成器的输出中效果不佳。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.