一个新的Python问题 [英] A new to Python question

查看:67
本文介绍了一个新的Python问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力教自己python,到目前为止还不错,但是我有一点麻烦让我觉得应该按照我认为的方式运行一个功能

工作。现在我正在使用我在Fortran中编写的一个简单程序和

尝试用Python编写。我得到了它的工作,但现在我正在努力

模块化它。我的fortran程序使用子程序,我正在尝试使用
在Python中执行相同的操作。但我还是新人,所以我很难理解我做错了什么。这是我的代码:


#! / usr / bin / python

#这个程序需要两个向量并将它们相乘

#against一个10X10数组。然后它还给出了点积,

#sum和数组的最大值。


导入数字

def abc (array1,array2,dotprod,sum,maxvalue):

"""取两个数组并执行预定的计算,

然后以相同的方式返回解决方案数组,沿着
与数组的点积,和和最大值。""

#TODO:让这个该死的东西工作!!

print" Array 1 in:",array1 #Debug

data = Numeric.zeros((10,10))

for i在范围(10)中:

范围内的j(10):

data [i,j] =(i + 1)+(j + 1)


e = Numeric.matrixmultiply(data,array1)

g = Numeric.matrixmultiply(Numeric.transpose(data),arr ay2)

array1 = e

array2 = g

dotprod = Numeric.dot(array1,array2)

sum = Numeric.sum(array1)

maxvalue = array2 [Numeric.argmax(array2)]

print" Array 1 out:&q uot;,array1 #Debug


返回array1,array2,dotprod,sum,maxvalue#<< - 不能正常工作

认为它会。


x = Numeric.arange(1,11)

y = Numeric.arange(1,11)* 2

dotp ,sumx,maxv = 0,0,0#这是声明变量的唯一方法吗?


打印''数组X:'',x

打印''数组Y:'',y

abc(x,y,dotp,sumx,maxv)

print''调用函数abc''

打印''数组X:'',x

打印''数组Y:'',y

打印''X和Y的点积:'',dotp

print''数组X的总和:'',sumx

print''数组Y的最大值:'',maxv


如果你运行它,数据就会很好地传递给函数,并且它会找到正确的数字。它只是让它传递给那些

无法正常工作。我在函数中放了一些print语句,只是为了看看数据是如何通过的。那么任何有助于我的想法?如果你想要看到fortran代码只想给我发电子邮件。


David

Hi I''m trying to teach myself python and so far to good, but I''m having
a bit of trouble getting a function to work the way I think it should
work. Right now I''m taking a simple program I wrote in Fortran and
trying to do it in Python. I got it to work, but now I''m trying to
modularize it. My fortran program uses a subroutine and I was trying
to do the same thing in Python. But I''m still new so I''m having
trouble understanding what I''m doing wrong. Here is my code:

#! /usr/bin/python
#This program takes two vectors and multiplies them
#against a 10X10 array. It also then gives the dot product,
#sum, and max value of the array.

import Numeric
def abc(array1,array2,dotprod,sum,maxvalue):
"""Takes two arrays and performs predetermined calculations,
Then returns the solutions back as the same array, along
with the dot product, sum, and max value of the array."""
#TODO: Get this damn thing working!!
print "Array 1 in:",array1 #Debug
data = Numeric.zeros((10,10))
for i in range(10):
for j in range(10):
data[i,j] = (i+1)+(j+1)

e = Numeric.matrixmultiply(data, array1)
g = Numeric.matrixmultiply(Numeric.transpose(data),arr ay2)
array1 = e
array2 = g
dotprod = Numeric.dot(array1,array2)
sum = Numeric.sum(array1)
maxvalue = array2[Numeric.argmax(array2)]
print "Array 1 out:",array1 #Debug

return array1,array2,dotprod,sum,maxvalue #<<-- Not working as I
thought it would.

x = Numeric.arange(1,11)
y = Numeric.arange(1,11)*2
dotp,sumx,maxv = 0,0,0 #Is this the only way to declare a variable?

print ''Array X:'',x
print ''Array Y:'',y
abc(x,y,dotp,sumx,maxv)
print ''Calling function abc''
print ''Array X:'',x
print ''Array Y:'',y
print ''Dot Product of X and Y:'',dotp
print ''Sum of array X:'',sumx
print ''Max value of array Y:'',maxv

If you run it the data gets passed to the function just fine and it
finds the right numbers. Its just getting it to pass them back thats
not working. I put some print statements inside the function just to
see how the data gets passed. So any ideas that would help me? If you
want to see the fortran code just email me.

David

推荐答案

2005-05-14,David写道:
On 2005-05-14, David wrote:
abc(x,y,dotp,sumx,maxv)
abc(x,y,dotp,sumx,maxv)




(x,y,dotp,sumx,maxv)= abc(x,y,dotp,sumx,maxv)


Bernd


-

那些想要放弃自由以获得安全的人,

不会有,也不值得他们得到, 任意一个。 [T.杰斐逊]



(x,y,dotp,sumx,maxv) = abc(x,y,dotp,sumx,maxv)


Bernd

--
Those who desire to give up freedom in order to gain security,
will not have, nor do they deserve, either one. [T. Jefferson]





你在想你是把参数作为参考传递(看看mutable

vs non-mutable)


你的函数返回一个tupple(x,y,...)中的值;你需要从那个tupple中获取

的价值


问候,


Philippe


大卫写道:
Hi,

You''re thinking you''re passing the arguments as reference (look at mutable
vs non-mutable)

Your function returns the values in a tupple (x,y,...); you need to fetch
the values from that tupple

Regards,

Philippe

David wrote:
嗨我正在努力教自己蟒蛇到目前为止很好,但我有点/让功能按照我认为应该工作的方式工作的麻烦。现在我正在使用我在Fortran中编写的一个简单程序,并尝试在Python中执行此操作。我得到了它的工作,但现在我正在尝试模块化它。我的fortran程序使用子程序,我正在尝试用Python做同样的事情。但我还是新人,所以我很难理解我做错了什么。这是我的代码:

#! / usr / bin / python
#This程序需要两个向量并将它们相乘
#against一个10X10数组。然后它还给出了点积,
#sum和数组的最大值。

import Numeric
def abc(array1,array2,dotprod,sum,maxvalue):
"""取两个数组并执行预定的计算,然后将解决方案作为相同的数组返回,并使用数组的点积,和值和最大值返回。"""
#TODO:让这个该死的东西工作!!
print" Array 1 in:",array1 #Debug
data = Numeric.zeros((10 ,10))
对于范围(10)中的i:对于范围(10)中的j:
数据[i,j] =(i + 1)+(j + 1)

e = Numeric.matrixmultiply(data,array1)
g = Numeric.matrixmultiply(Numeric.transpose(data),arr ay2)
array1 = e
array2 = g
dotprod = Numeric.dot(array1,array2)
sum = Numeric.sum(array1)
maxvalue = array2 [Numeric.argmax(array2)]
print"数组1输出:,array1 #Deb ug

返回array1,array2,dotprod,sum,maxvalue#<< - 不工作,因为我认为它会。

x =数字。 arange(1,11)
y = Numeric.arange(1,11)* 2
dotp,sumx,maxv = 0,0,0#这是声明变量的唯一方法吗? />
打印''数组X:'',x
打印''数组Y:'',y
abc(x,y,dotp,sumx,maxv)
print''调用函数abc''
打印''数组X:'',x
打印''数组Y:'',y
打印''X和Y的点积: '',dotp
打印''数组X的总和:'',sumx
打印''数组Y的最大值:'',maxv

如果你运行它数据传递给函数就好了,它找到了正确的数字。它只是让它传递给那些
无法正常工作。我在函数内部放了一些print语句,以便了解数据是如何传递的。那么任何有助于我的想法?如果您想看到fortran代码,请给我发电子邮件。

David
Hi I''m trying to teach myself python and so far to good, but I''m having
a bit of trouble getting a function to work the way I think it should
work. Right now I''m taking a simple program I wrote in Fortran and
trying to do it in Python. I got it to work, but now I''m trying to
modularize it. My fortran program uses a subroutine and I was trying
to do the same thing in Python. But I''m still new so I''m having
trouble understanding what I''m doing wrong. Here is my code:

#! /usr/bin/python
#This program takes two vectors and multiplies them
#against a 10X10 array. It also then gives the dot product,
#sum, and max value of the array.

import Numeric
def abc(array1,array2,dotprod,sum,maxvalue):
"""Takes two arrays and performs predetermined calculations,
Then returns the solutions back as the same array, along
with the dot product, sum, and max value of the array."""
#TODO: Get this damn thing working!!
print "Array 1 in:",array1 #Debug
data = Numeric.zeros((10,10))
for i in range(10):
for j in range(10):
data[i,j] = (i+1)+(j+1)

e = Numeric.matrixmultiply(data, array1)
g = Numeric.matrixmultiply(Numeric.transpose(data),arr ay2)
array1 = e
array2 = g
dotprod = Numeric.dot(array1,array2)
sum = Numeric.sum(array1)
maxvalue = array2[Numeric.argmax(array2)]
print "Array 1 out:",array1 #Debug

return array1,array2,dotprod,sum,maxvalue #<<-- Not working as I
thought it would.

x = Numeric.arange(1,11)
y = Numeric.arange(1,11)*2
dotp,sumx,maxv = 0,0,0 #Is this the only way to declare a variable?

print ''Array X:'',x
print ''Array Y:'',y
abc(x,y,dotp,sumx,maxv)
print ''Calling function abc''
print ''Array X:'',x
print ''Array Y:'',y
print ''Dot Product of X and Y:'',dotp
print ''Sum of array X:'',sumx
print ''Max value of array Y:'',maxv

If you run it the data gets passed to the function just fine and it
finds the right numbers. Its just getting it to pass them back thats
not working. I put some print statements inside the function just to
see how the data gets passed. So any ideas that would help me? If you
want to see the fortran code just email me.

David






2005 -05-14,Philippe C. Martin写道:
On 2005-05-14, Philippe C. Martin wrote:
你在想你是把参数作为参考传递
You''re thinking you''re passing the arguments as reference




这就是Fortran处理它们的方式:


[...]



That is the way Fortran handles them:

[...]

现在我正在采用我在Fortran中编写的简单程序
Right now I''m taking a simple program I wrote in Fortran




Bernd


- -

那些为了获得安全而放弃自由的人,不管是b $ b还是他们都不应该得到任何一个。 [T.杰斐逊]




Bernd

--
Those who desire to give up freedom in order to gain security,
will not have, nor do they deserve, either one. [T. Jefferson]


这篇关于一个新的Python问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆