Select to view content in your preferred language

How to get the MIN and Max from a while loop - python

9662
14
Jump to solution
08-03-2012 09:44 AM
DanielAbera
Occasional Contributor
Hi Eveyone,

I got the following python code to get the geometry(X, Y, Z) of a polyline feature. It can print all the values. But what I am looking is that, how do we get the MIN and MAX values for Z -value which is an elevation value in my case; and pass it as a variable. I am new to python and having trouble to figure it out. The code is as follows. Thanks in advance for your help.

Daniel A.

# Import native arcgisscripting module # import arcgisscripting  # Create the geoprocessor object # gp = arcgisscripting.create(9.3)  infc = gp.GetParameterAsText(0)    # Identify the geometry field # desc = gp.Describe(infc) shapefieldname = desc.ShapeFieldName  # Create search cursor # rows = gp.SearchCursor(infc) row = rows.Next()  # Enter while loop for each feature/row # while row:     # Create the geometry object     #     feat = row.GetValue(shapefieldname)      # Print the current multipoint's ID     #     print "Feature " + str(row.getvalue(desc.OIDFieldName)) + ":"     partnum = 0      # Count the number of points in the current multipart feature     #     partcount = feat.PartCount      # Enter while loop for each part in the feature (if a singlepart feature     # this will occur only once)     #     while partnum < partcount:         # Print the part number         #         print "Part " + str(partnum) + ":"         part = feat.GetPart(partnum)         pnt = part.Next()         pntcount = 0          # Enter while loop for each vertex         #         while pnt:             # Print x,y coordinates of current point             #             print pnt.x, pnt.y, pnt.z             pnt = part.Next()             pntcount += 1              # If pnt is null, either the part is finished or there is an              #   interior ring             #             if not pnt:                  pnt = part.Next()                 if pnt:                     print "Interior Ring:"         partnum += 1      row = rows.Next()
Tags (2)
0 Kudos
14 Replies
FerialAssmani
Emerging Contributor
Dear all, For my case I am doing a simple scripting with Python for a Abaqus odb reading, so i want to extract the max value and its Node label of my list (reading via a loop)
#
for v in centerDisplacement.values:
    print 'Node label          = ', v.nodeLabel
    print 'X displacement = ', v.data[0]
#
-----------------------------
Node label          = 1
X displacement =0.000
Node label          = 2
X displacement =1.050
Node label          = 3
X displacement =2.306
--------------------------------
So, I want to print at next

Max displacement = 2.306
For the Node label= 3

Thanks
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Ferial,

You could accomplish this using a dictionary:

dict = {}

for v in centerDisplacement.values:
    dict[v.nodeLabel] = v.data[0]    

#sort dictionary
list = sorted(dict, key=dict.get)
max =  list[-1]

print 'Max displacement = ', dict[max]
print 'For the Node label = ', max
0 Kudos
FerialAssmani
Emerging Contributor
Hi JSkinn3, Thank you it work 10/10
Please for the lower case (min value), is it correct to write ?
*************
dict = {}

for v in centerDisplacement.values:
    dict[v.nodeLabel] = v.data[0]   

#sort dictionary
list = sorted(dict, key=dict.get)
max =  list[-1]
min =  list[0]

print 'Max displacement = ', dict[max]
print 'For the Node label = ', max

print 'Min displacement = ', dict[min]
print 'For the Node label = ', min
*************
Thanks a lot
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Yes, that is correct.  The MIN value will be the first value in the list, i.e. list[0].
0 Kudos
FerialAssmani
Emerging Contributor
Lot of thanks JSkinn3 😉
0 Kudos