解决输入文件数据中的最大值和最小值的问题

发布于 2024-10-14 02:40:29 字数 3844 浏览 6 评论 0原文

我是 python 新手,我需要一些有关名为 search_max.py 的 python 脚本的帮助。

它打开一个“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

预先感谢您, 埃希利安。

I am new with python and i need a little help with my python script named search_max.py.

It opens a file "xyz" format and then search for the min and max of each coord. The problem is when i do the same with an awk script i don't get the same resuts!!!

I wonder if there is a problem with the type of data or string operation or ... Can anyone help me solve this problem?

Python script :

#!/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 script :

#!/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}

you can download my input file here (14 days): peamorphe.xyz

Thank you in advance,
Exilien.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

<逆流佳人身旁 2024-10-21 02:40:29

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

看起来比较容易,你不觉得吗?

The reason why your code does not work as expected is given by eumiro in his above comment.

There is an far easier approach for this in Python though: Use NumPy. Example code for maxima and minima of each column of your file would be

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

These few lines of code do everything your script does, including parsing the input file. If you also need the indices of the maxima and minima, you can use a.argmax() and a.argmin().

Looks quite a bit easier, don't you think?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文