在python中读取大量数字

时间:2015-09-30 09:36:11

标签: python

我正在尝试读取大量数字(总共8112个)并将它们重新排列为6列。首先,我想在第一列添加52个数字,然后我想在第二列中添加52,然后在第三列中添加52,依此类推。当我得到6列,每列包含52个数字时,我想继续以相同的方式读取,直到数据结束。我试过这个:

Index error: list index out of range

代码没有正确读取数字,直到最后都没有。在阅读了大约7000个数字后,它正在弯腰。我得到一个-0.001491728991 -0.001392067804 -0.001383514062 -0.000777354202 -0.000176516325 -0.00066003232 0.001491728657 0.001392067465 0.00138351373 0.00077735388 0.000176516029 0.000660032023 -0.001491728966 -0.001392067669 -0.001383513988 -0.000777354111 -0.000176516303 2.5350931e-05 -0.000660032277 0.001491728631 0.00139206733 0.001383513657 0.000777353789 0.000176516006 0.000660031981 -0.003692742099 -0.003274685372 -0.001504168916 0.003692740966 0.003274684254 0.001504167874 -0.003692741847 -0.003274685132 -0.001504168791 (...)

输入文件包含如下所示的数字:

typedef long long int int64_cu typedef unsigned long long int uint64_cu

(总共8112个数字)

1 个答案:

答案 0 :(得分:0)

试试这个:

data = range(8112) # replace with input from file

col_size = 52
col_count = 6
batch_size = (col_size*col_count)
# split input into batches of 6 columns of 52 entries each
for batch in range(0,len(data),batch_size):
    # rearrange line data into 6 column format
    cols = zip(*[data[x:x+col_size] for x in range(batch,batch+batch_size,col_size)])
    for c in cols:
        print c

输出:

(0, 52, 104, 156, 208, 260)
(1, 53, 105, 157, 209, 261)
...
(50, 102, 154, 206, 258, 310)
(51, 103, 155, 207, 259, 311)
(312, 364, 416, 468, 520, 572)
(313, 365, 417, 469, 521, 573)
...
(362, 414, 466, 518, 570, 622)
(363, 415, 467, 519, 571, 623)
(624, 676, 728, 780, 832, 884)
(625, 677, 729, 781, 833, 885)
...