【发布时间】:2012-12-10 15:00:02
【问题描述】:
需要从一个数组中取出值,通过一个函数将它们放入另一个数组中。这意味着使用一对嵌套的 for 循环来完成。请帮忙。在这里完成初学者。
编辑:好的,澄清一下,我有一个包含各种值的二维数组。我想对所有这些值应用一个函数,并在它们通过函数后返回一个带有值的二维数组。我在 python 中工作。感谢您的快速回复以及您可以提供的任何帮助!
EDIT3:示例代码:
import numpy as N
def makeGrid(dim):
''' Function to return a grid of distances from the centre of an array.
This version uses loops to fill the array and is thus slow.'''
tabx = N.arange(dim) - float(dim/2.0) + 0.5
taby = N.arange(dim) - float(dim/2.0) + 0.5
grid = N.zeros((dim,dim), dtype='float')
for y in range(dim):
for x in range(dim):
grid[y,x] = N.sqrt(tabx[x]**2 + taby[y]**2)
return grid
import math
def BigGrid(dim):
l= float(raw_input('Enter a value for lambda: '))
p= float(raw_input('Enter a value for phi: '))
a = makeGrid
b= N.zeros ((10,10),dtype=float) #Create an arry to take the returned values
for i in range(10):
for j in range (10):
b[i,j] = a[i][j]*2
if __name__ == "__main__":
''' Module test code '''
size = 10 #Dimension of the array
newGrid = BigGrid(size)
newGrid = N.round(newGrid, decimals=2)
print newGrid
【问题讨论】:
-
两个数组与一个 '2d' 数组不同。你能更具体地说明你想做什么吗?也许分享你迄今为止尝试过的东西?你用什么语言工作?
-
我想说的描述很模糊
-
你的两个数组是二维的吗?
-
是二维数组。查看 EDIT3 中的代码。这有意义吗?
-
它给了我以下错误'函数'对象没有属性'getitem'
标签: python arrays for-loop numpy