我已经搜索了,并搜索了(4天),然后发布这篇文章。如果太初级,浪费你的时间,我会提前道歉。我已经通过使用他们的教程中的例子成功地生成了一些基本的绘图,以及matplotlib,但是对我需要完成的工作没有任何帮助。
实质上:
20例:
173
1685
1152
253
1623
390
84
40
319
86
54
991
1012
721
3074
4227
4927
181
4856
1415最后,我需要做的是计算单个总数的范围(平均分布在条目的绝对总数上),然后使用python的任何绘图库绘制这些平均值。为了方便使用,我考虑过使用pyplot。
ie:
Entries 1-5 = (plottedTotalA)
Entries 6-10 = (plottedTotalB)
Entries 11-15 = (plottedTotalC)
Entries 16-20 = (plottedTotalD)据我所知,我不需要无限期地存储变量的值,只在它们被处理时(按照顺序)传递给绘图仪。我尝试了下面的示例,从上面的20项列表(这是可行的)中将5个条目相加,但我不知道如何一次动态地传递5个条目,直到完成,同时保留最终将被传递给pyplot的计算平均值。
例:
Python 2.7.3 (default, Jul 24 2012, 10:05:38)
[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> plottedTotalA = ['173', '1685', '1152', '253', '1623']
>>> sum(float(t) for t in plottedTotalA)
4886.0发布于 2013-02-11 17:37:35
让我们假设您的n值在一个名为x的列表中,然后将x重新组合成一个包含5列的数组A,并计算每一行的平均值。然后,您可以简单地绘制得到的向量。
x = np.array(x)
n = x.size
A = x[:(n // 5) * 5].reshape(5, -1)
y = A.mean(axis = 0)
plot(y)编辑:根据tacaswell的评论更改了我的代码
但是,如果实际有超过100万条条目,则可能会遇到内存问题。您也可以使用名称x代替A和y,这样就可以覆盖初始数据并节省一些内存。
我希望这能帮到你
发布于 2013-02-11 18:14:26
我认为问题是如何从文件生成的列表中获取5项。正如你所说:
我不知道如何在完成之前一次动态地传递5,
我使用/dev/random,因为它永远不会结束和随机,并模拟您的大文件,并显示处理一个大文件,而不读取到列表或类似的数据消耗。
################################################################################
def bigfile():
"""Never ending list of random numbers"""
import struct
with open('/dev/random') as f:
while True:
yield struct.unpack("H",f.read(2))[0]
################################################################################
def avg(l):
"""Noddy version"""
return sum(l)/len(l)
################################################################################
bigfile_i = bigfile()
import itertools
## Grouper recipe @ itertools
by_5 = itertools.imap(None, *[iter(bigfile_i)]*5)
# Only take 5, 10 times.
for x in range(10):
l = by_5.next()
a = avg(l)
print l, a ## PLOT ?编辑
其余部分发生的细节。
如果我们假装该文件有11行,并且每次取5行:
In [591]: list(itertools.izip_longest(*[iter(range(11))]*5))
Out[591]: [(0, 1, 2, 3, 4), (5, 6, 7, 8, 9), (10, None, None, None, None)]
In [592]: list(itertools.imap(None, *[iter(range(11))]*5))
Out[592]: [(0, 1, 2, 3, 4), (5, 6, 7, 8, 9)]
In [593]: list(itertools.izip(*[iter(range(11))]*5))
Out[593]: [(0, 1, 2, 3, 4), (5, 6, 7, 8, 9)]在一种情况下,izip_longest将用None填充剩余部分,而imap和izip将截断。我可以想象OP可能希望使用itertools.izip_longest(*iterables[,fillvalue])作为可选的fill值,尽管None是No Values的一个很好的哨兵。
我希望这能清楚地说明其余部分的情况。
https://stackoverflow.com/questions/14817034
复制相似问题