python 从输入文件数据求解最大值和最小值时出现问题

8qgya5xd  于 2023-01-29  发布在  Python
关注(0)|答案(1)|浏览(205)

我是python新手,我需要一点帮助来使用我的python脚本search_max. py。
它打开一个xyz格式的文件,然后搜索每个坐标的最小值和最大值。问题是当我用awk脚本做同样的事情时,我没有得到同样的结果!!!
我想知道是数据类型或字符串操作有问题还是...有谁能帮我解决这个问题吗?

Python脚本:

#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
inputfile = "peamorphe.xyz"
outputfile = "result.txt"
# open the input file
infile = open(inputfile, "r")
# read line 1 : number of atoms
atomsno = infile.readline().rstrip('\n').split(" ")
# read line 2 : name of the system
systemname = infile.readline().rstrip('\n')
# read line 3 : initialisation for min and max
temp2 = infile.readline().rstrip('\n').split(" ")
zmin = temp2[3]
zmax = temp2[3]
ymax = temp2[2]
ymin = temp2[2]
xmax = temp2[1]
xmin = temp2[1]
lineno = 3
print zmax, ymin, xmin
# read other lines
for ligne in infile.readlines():
    lineno = lineno + 1
    # extraction and strip of data spaced by " "
    data = ligne.rstrip('\n\r').split(" ")
    # Conditions for min and max
    if data[1] < xmin: 
        xmin = data[1] 
        wclxmin = lineno
    if data[1] > xmax: 
        xmax = data[1]
        wclxmax = lineno
    if data[2] < ymin: 
        ymin = data[2]
        wclymin = lineno
    if data[2] > ymax: 
        ymax = data[2]
        wclymax = lineno
    if data[3] < zmin:
        zmin = data[3]
        wclzmin = lineno
    if data[3] > zmax: 
        zmax = data[3] 
        wclzmax = lineno

# Evaluation of centers
zcenter = float(zmax)-float(zmin)
ycenter = float(ymax)-float(ymin)
xcenter = float(xmax)-float(xmin)

# open the input file
infile = open(inputfile, "r")
# read line 1 : number of atoms
atomsno = infile.readline().rstrip('\n').split(" ")
# read line 2 : name of the system
systemname = infile.readline().rstrip('\n')
# read line 3 : initialisation for min and max
temp2 = infile.readline().rstrip('\n').split(" ")
zmin = temp2[3]
zmax = temp2[3]
ymax = temp2[2]
ymin = temp2[2]
xmax = temp2[1]
xmin = temp2[1]
lineno = 3
print zmax, ymin, xmin
# read other lines
for ligne in infile.readlines():
    lineno = lineno + 1
    # extraction and strip of data spaced by " "
    data = ligne.rstrip('\n\r').split(" ")
    # Conditions for min and max
    if data[1] < xmin: 
        xmin = data[1] 
        wclxmin = lineno
    if data[1] > xmax: 
        xmax = data[1]
        wclxmax = lineno
    if data[2] < ymin: 
        ymin = data[2]
        wclymin = lineno
    if data[2] > ymax: 
        ymax = data[2]
        wclymax = lineno
    if data[3] < zmin:
        zmin = data[3]
        wclzmin = lineno
    if data[3] > zmax: 
        zmax = data[3] 
        wclzmax = lineno

# Evaluation of centers
zcenter = float(zmax)-float(zmin)
ycenter = float(ymax)-float(ymin)
xcenter = float(xmax)-float(xmin)

awk脚本:

#!/usr/bin/awk -f
# from a xyz file
BEGIN{
xmax;xmin;
zmax;zmin;
ymax;ymin;
xcent;ycent;zcent;
xcent = (xmax-xmin)/2;
ycent = (ymax-ymin)/2;
zcent = (zmax-zmin)/2;
print   "At the start of the script";
print   "xmax = " xmax "; " "xmin = " xmin "; xcent = " xcent; 
print   "ymax = " ymax "; " "ymin = " ymin "; ycent = " ycent;
print   "zmax = " zmax "; " "zmin = " zmin "; zcent = " zcent;
print   "";
}

{
if (xmax<$2) xmax = $2
if (xmin>$2) xmin = $2
if (ymax<$3) ymax = $3
if (ymin>$3) ymin = $3
if (zmax<$4) zmax = $4
if (zmin>$4) zmin = $4
}

END{
xcent = (xmax-xmin)/2;
ycent = (ymax-ymin)/2;
zcent = (zmax-zmin)/2;
print   "At the end of the script";
print   "xmax = " xmax "; " "xmin = " xmin "; xcent = " xcent; 
print   "ymax = " ymax "; " "ymin = " ymin "; ycent = " ycent;
print   "zmax = " zmax "; " "zmin = " zmin "; zcent = " zcent}

你可以下载我的输入文件在这里(14天):peamorphe.xyz
先谢谢你了,艾希林。

8xiog9wr

8xiog9wr1#

eumiro在上面的注解中给出了代码不能按预期工作的原因。
不过,在Python中有一种简单得多的方法:使用NumPy。文件中每列的最大值和最小值的示例代码如下

import numpy
a = numpy.loadtxt("peamorphe.xyz", skiprows=2, usecols=(1, 2, 3))
max_xyz = a.max(axis=0)
min_xyz = a.min(axis=0)
center = max_xyz - min_xyz

这几行代码完成了脚本所做的一切,包括解析输入文件,如果还需要最大值和最小值的索引,可以使用a.argmax()a.argmin()
看起来容易多了,你不觉得吗?

相关问题