我有以下代码。我有一个矩阵,我想一次取两行并放入这些函数中。我已经查看了 intertools,但我不确定它们是否可以在这种情况下工作。我尝试了 islice,但它没有给出我想要的输出。我知道我必须为“方法”提供位置参数,但我将它们排除在外,因为这段代码会给出结果,它们只是相同的最小值,最大值等等。我正在尝试给我有关索引错误和不可下标的对象的问题。我是 python 新手,我正在阅读一些书籍,但它们没有直接涵盖这一点。我查看了一些人在 stackoverflow 上寻找的内容,但他们主要想一次读取 2 行文件,而不是 numpy ndarrays。任何建议表示赞赏。
import numpy as np
def xmax():
for i in range(1):
setOne = (matrix[i:1])
setTwo = (matrix[i+1:2])
if setOne[i][0:1] > setTwo[i][0:1]:
xMax = setOne[i][0:1]
else:
xMax = setTwo[i][0:1]
return(xMax)
def xmin():
for i in range(1):
setOne = (matrix[i:1])
setTwo = (matrix[i+1:2])
if setOne[i][0:1] < setTwo[i][0:1]:
xMin = setOne[i][0:1]
else:
xMin = setTwo[i][0:1]
return(xMin)
def ymax():
for i in range(1):
setOne = (matrix[i:1])
setTwo = (matrix[i+1:2])
if setOne[i][1:2] > setTwo[i][1:2]:
yMax = setOne[i][1:2]
else:
yMax = setTwo[i][1:2]
return(yMax)
def ymin():
for i in range(1):
setOne = (matrix[i:1])
setTwo = (matrix[i+1:2])
if setOne[i][1:2] < setTwo[i][1:2]:
yMin = setOne[i][1:2]
else:
yMin = setTwo[i][1:2]
return(yMin)
def zmax():
for i in range(1):
setOne = (matrix[i:1])
setTwo = (matrix[i+2:3])
if setOne[i][2:3] > setTwo[i][2:3]:
zMax = setOne[i][2:3]
else:
zMax = setTwo[i][2:3]
return(zMax)
def zmin():
for i in range(1):
setOne = (matrix[i:1])
setTwo = (matrix[i+2:3])
if setOne[i][2:3] < setTwo[i][2:3]:
zMin = setOne[i][2:3]
else:
zMin = setTwo[i][2:3]
return(zMin)
'''***********************************************************************************
Created on Jan 27, 2013
@author:
***********************************************************************************'''
f ='/Users/Documents/workspace/findMinMax/crapc.txt'
x,y,z = np.loadtxt(f, unpack=True, usecols=(1,2,3), ndmin = 2)
maxZ = max(z)
minZ = min(z)
print("Maximum Z value: " + str(maxZ))
print("Minimum Z value: " + str(minZ))
matrix = [x,y,z]
matrix = np.rot90(matrix)
matrix = matrix[::-1]
"""for each_row in range(0,len(matrix)-1,2):
print("Xmax:" + str(xmax()))
print("Xmin:" + str(xmin()))
print("Ymax:" + str(ymax()))
print("Ymin:" + str(ymin()))
print("Zmax:" + str(zmax()))
print("Zmin:" + str(zmin()))"""