Python Data Structure Recommendations for a 2D Grid -
i looking implement ant colony optimization algorithm in python, though new both python , object oriented programming learning curve has been rather steep. @ point, stuck how address following situation:
- as ants walk around 2d grid, encounter obstacles, pheromone deposits other ants, food, etc. data structure use represent 2d world , aforementioned properties of each cell?
i had tried 2d array, thinking array[x-coord][y-coord]
point {} (dictionary)
appropriate properties (obstacle: 'yes / 'no', pheromone level: x %, etc.)
. unfortunately, though numpy lets me create 2d array, cannot assign dictionary objects various coordinates.
from numpy import * myarray = array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) myarray[2][2]={}
returns:
traceback (most recent call last): file "/users/amormachine/desktop/pythontest.py", line 7, in <module> myarray[2][2]={} typeerror: long() argument must string or number, not 'dict' [finished in 0.6s exit code 1]
i not committed either dictionaries or paradigm implementing project , appreciate wisdom of group.
sure can, cant if dtype int ... make array objects , can use objects...
in [43]: = [[{},{},{}],[{},{},{}]] in [44]: = numpy.array(a) in [45]: a[1][1] = {'hello':'world','something':5} in [46]: out[46]: array([[{}, {}, {}], [{}, {'hello': 'world', 'something': 5}, {}]], dtype=object)
although not sure whay gain using numpy objects, may better off leaving list of lists
Comments
Post a Comment