我们基本上有一个大的xcel文件,我想要做的是创建一个列表,其中包含每列的最大值和最小值。有13列,这就是为什么while循环应该在它达到14时停止的原因。问题是一旦计数器增加,它似乎不会迭代for循环一次。或者更明确地说,while循环只通过for循环一次,但它似乎循环,它将计数器增加1并停止在14.应该注意输入文件中的行是数字的字符串,这是为什么我将它们转换为元组,而不是检查给定位置的值是否大于column_max或小于column_min。如果是这样,我重新分配column_max或column_min。一旦完成,column_max和column_min被附加到列表(l),并且计数器(位置)被增加以重复下一列。任何帮助将不胜感激。
input_file = open('names.csv','r')
l= []
column_max = 0
column_min = 0
counter = 0
while counter<14:
for row in input_file:
row = row.strip()
row = row.split(',')
row = tuple(row)
if (float(row[counter]))>column_max:
column_max = float(row[counter])
elif (float(row[counter]))<column_min:
column_min = float(row[counter])
else:
column_min=column_min
column_max = column_max
l.append((column_max,column_min))
counter = counter + 1
答案 0 :(得分:3)
我认为您想要切换for
和while
循环的顺序。
请注意,有一种略微更好的方法:
with open('yourfile') as infile:
#read first row. Set column min and max to values in first row
data = [float(x) for x in infile.readline().split(',')]
column_maxs = data[:]
column_mins = data[:]
#read subsequent rows getting new min/max
for line in infile:
data = [float(x) for x in line.split(',')]
for i,d in enumerate(data):
column_maxs[i] = max(d,column_maxs[i])
column_mins[i] = min(d,column_mins[i])
如果你有足够的内存来同时将文件保存在内存中,这就变得更加容易了:
with open('yourfile') as infile:
data = [map(float,line.split(',')) for line in infile]
data_transpose = zip(*data)
col_mins = [min(x) for x in data_transpose]
col_maxs = [max(x) for x in data_transpose]
答案 1 :(得分:1)
消耗完文件后,它就被消耗了。因此,再次迭代它不会产生任何结果。
>>> for row in input_file:
... print row
1,2,3,...
4,5,6,...
etc.
>>> for row in input_file:
... print row
>>> # Nothing gets printed, the file is consumed
这就是您的代码无效的原因。
然后您有三种主要方法:
min
和max
定)。这是第三种方法的技巧:
maxima = [float('-inf')] * 13
minima = [float('inf')] * 13
with open('names.csv') as input_file:
for row in input_file:
for col, value in row.split(','):
value = float(value)
maxima[col] = max(maxima[col], value)
minima[col] = min(minima[col], value)
# This gets the value you called ``l``
combined_max_and_min = zip(maxima, minima)