Python 3.1 - Grid simulation
I'm hoping to get some tips on what direction I should take, to complete the following;
I need to take a 1D list of integers, and convert it into a grid; w开发者_Go百科here the integers are randomly placed (multi-dimensional array?). I then need a function, where when I select and then modify one of the grid elements, all surrounding elements will be affected also.
Thank you in advance for your time!
Well, here's a quick one:
1d_list = [45, 18, 77, 24, 47, 88, 61, 96, 46, 97, 16, 65, 39, 56, 14, 48, 87, 44, 91, 22, 21, 38, 59, 95, 75, 80, 20, 51, 66, 62, 50, 82, 60, 57, 10, 23, 6, 8, 28, 83, 71, 76, 36, 32, 90, 29, 40, 37] // I didn't write this ;)
2d_list = []
for y in range(6):
holder = []
for x in range(8):
holder.append(x)
2d_list.append(y)
2d_list[y].append(holder)
Now, you can just use 2d_list
like you would a normal coordinate system:
print 2d_list[0][4] // Output: 47
Good luck. If it's going to be big, you'll need to use Python Arrays, as Lists and Dictionaries just won't cut it any more due to speed issues (I might be mixed up. There should be a C array implementation in Python, right?).
精彩评论