Schreiben von Code in Python, um eine grundlegende Funktion zu bewerten. Ich habe ein 2D-Array mit einigen Werten, und ich möchte die Funktion jeder dieser Werte anzuwenden und eine neue 2D-Array erhalten:TypeError: 'Funktion' Objekt hat kein Attribut '__getitem__'
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 array to take the returned values
for i in range(10):
for j in range (10):
b[i][j] = a[i][j]*l*p
return b
if __name__ == "__main__":
''' Module test code '''
size = 10 #Dimension of the array
newGrid = BigGrid(size)
newGrid = N.round(newGrid, decimals=2)
print newGrid
Aber alles, was ich bekommen, ist die Fehlermeldung
Traceback (most recent call last):
File "sim.py", line 31, in <module>
newGrid = BigGrid(size)
File "sim.py", line 24, in BigGrid
b[i][j] = a[i][j]*l*p
TypeError: 'function' object has no attribute '__getitem__'
Bitte helfen.
einige allgemeine Bemerkungen über deinen numpy Code: 1) versuche "numpy als np" zu importieren, als tha Das ist die numpige Konvention. 2) Benutze Vektoroperationen, dh: a = b * l * p 'anstelle der Doppelschleife. Es wird Hunderte oder Tausende Male schneller sein, und es ist einfacher zu lesen. 3) Indexiere keine numpigen Arrays als 'array [i] [j]', benutze stattdessen 'array [i, j] 'es ist viel schneller und kürzer zu schreiben;). Um es zusammenzufassen, lesen Sie ein numpliges Tutorial. – jorgeca