我有以下代码:
import numpy as np
import scipy as sp
import itertools
import os
rootdir = 'mydir'; # mydir changed from actual directory
data = []
for i in range(1, 35):
data = []
chari = str(i)+'/';
newdir = rootdir+chari
print newdir
for root, dirs, files in os.walk(newdir):
for fname in files:
if fname == 'tc.out':
with open(os.path.join(root,fname)) as f:
lines_after_2 = f.readlines()[2:]
for line in lines_after_2:
fields = line.split()
rowdata = map(float,fields)
data.extend(rowdata)
column = zip(*rowdata)
for index, column in enumerate(columns):
print "In column %s, Max = %s, Min = %s" % (index, max(column), min(column))
我试图找到每列的MAXIMUM和MINIMUM值(我的真正目标是找到每一列的最大值和最小值,但第一列,但我稍后会尝试解决这个问题。)
运行此代码后,出现以下错误:
TypeError: zip argument #1 must support iteration
这就是我的数据结构:
variables = time T_1 T_2 T_3 T_4 T_5
# 5
3.0100000000e+03 3.0349158928e+02 3.0171422239e+02 2.9770226435e+02 2.9657072961e+02 2.9707000000e+02
3.0125798902e+03 3.0334549013e+02 3.0152263068e+02 2.9799460866e+02 2.9662883748e+02 2.9706572397e+02
3.0175798902e+03 3.0324699921e+02 3.0138079877e+02 2.9821459880e+02 2.9667166944e+02 2.9706245240e+02
3.0225798902e+03 3.0336015983e+02 3.0128802830e+02 2.9836039823e+02 2.9670134577e+02 2.9706003491e+02
我试图跳过前两行,然后读取列并选出最大值和最小值。
这里发生了什么?
答案 0 :(得分:1)
您需要将column = zip(*rowdata)
更改为column = zip(*data)
。另外,如评论中所述,您应使用append
代替extend
:
rowdata = map(float,fields)
data.append(rowdata)
column = zip(*data)