Python - TypeError: zip argument #1 must support iteration

Python - TypeError: zip argument #1 must support iteration

我有以下代码:

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))

我试图找到每列的最大值和最小值(我的真正目标是找到除第一列以外的每一列的最大值和最小值,但我稍后会尝试解决)。

根据 运行 这段代码,我收到以下错误:

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

我试图跳过前两行,然后阅读列并选择最大值和最小值。

这是怎么回事?

您需要将 column = zip(*rowdata) 更改为 column = zip(*data)。另外如评论中所述,您应该使用 append 而不是 extend :

rowdata = map(float,fields)
data.append(rowdata)
column = zip(*data)