开发者

Translation from Perl to Python, brief

开发者 https://www.devze.com 2023-03-25 20:36 出处:网络
I really need help on this. I tried translating this Perl into Python, but it doesn\'t seem to do what I want.

I really need help on this. I tried translating this Perl into Python, but it doesn't seem to do what I want.

Thanks!

Perl:

@X_max = &MaxValue(@xCoords);
     $maxX = int(10 * $X_max[0]/5);
    @X_min = &MinValue(@xCoords);
    $minX =  int(10 * $X_min[0]/5);
    $rangeX = &Range($minX, $maxX);
    print GRID "X:\t $maxX\t $minX\t\t $rangeX\n";

Python (so far):

_max = max(xCoords) 
max(X) = int(10 * max((X)0/5))
X_min = min(xcoords)
min(X) = int(10 * min((X开发者_如何学JAVA)0/5))
range(X) = range(min(X), max(X))
print('X:') ('%4s') % min, '%15s' % max, '%25s' % range


The Perl does not look like it's simply the Python min, max, and range functions. Because MaxValue returns a list of values, which is disregarded in the next step as the code simply takes the first item in the list. So it might be something like this:

x_max  = max( xcoords );
maxx   = int( 10 * x_max / 5 );
x_min  = min( xcoords );
minx   = int( 10 * x_min / 5 );
rangex = range( minx, maxx );
grid.write(  "X:\t{0}\t{1}\t{2}\t{3}".format(minX, maxX, rangeX));

Or even:

rangex = range( 2 * min( xcoords ), 2 * max( xcoords ));

However, the range seems to indicate that it's simply a simple variable, perhaps just maxx - minx + 1. So I don't think Python range will work.

This is over-complex code, to say the least: 10 * x / 5 <=> 2 * x.


I suck at Perl, I don't know what does Range() do, but I tried and I wrote this:

X_max = []
X_min = []
for i in xCoords:
    X_max.append(i.max)
    X_min.append(i.min)
maxX = 10 * X_max[0] / 5
minX = 10 * X_min[0] / 5
rangeX = range(minX, maxX)
out = "X:\t{0}\t{1}\t{2}\t{3}".format(minX, maxX, rangeX)
#now you can do print out -OR- open('./file', 'w').write(out)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号