需要从一个数组中获取值,将它们放入一个函数并将它们放入另一个数组中。这意味着使用一对嵌套的 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