有點困惑。這是你試圖達到的目標嗎?
import random
def GridSet():
fullGrid = []
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
for j in range(10):
collect = []
for i in range(10):
collect_sub = []
collect_sub.append([letters[j] + repr(i+1), random.randrange(100)+1, 0])
collect.append(collect_sub)
fullGrid.append(collect)
return fullGrid
filledGrid = GridSet()
import pprint
pprint.pprint(filledGrid)
輸出
[[[['A1', 12, 0]],
[['A2', 99, 0]],
[['A3', 91, 0]],
[['A4', 29, 0]],
[['A5', 72, 0]],
[['A6', 24, 0]],
[['A7', 99, 0]],
[['A8', 77, 0]],
[['A9', 39, 0]],
[['A10', 93, 0]]],
[[['B1', 54, 0]],
[['B2', 78, 0]],
[['B3', 12, 0]],
[['B4', 78, 0]],
[['B5', 79, 0]],
[['B6', 68, 0]],
[['B7', 80, 0]],
[['B8', 29, 0]],
[['B9', 60, 0]],
[['B10', 48, 0]]],
[[['C1', 59, 0]],
[['C2', 20, 0]],
[['C3', 4, 0]],
[['C4', 42, 0]],
[['C5', 91, 0]],
[['C6', 61, 0]],
[['C7', 28, 0]],
[['C8', 68, 0]],
[['C9', 18, 0]],
[['C10', 73, 0]]],
[[['D1', 9, 0]],
[['D2', 29, 0]],
[['D3', 81, 0]],
[['D4', 46, 0]],
[['D5', 49, 0]],
[['D6', 95, 0]],
[['D7', 64, 0]],
[['D8', 5, 0]],
[['D9', 26, 0]],
[['D10', 88, 0]]],
[[['E1', 62, 0]],
[['E2', 6, 0]],
[['E3', 35, 0]],
[['E4', 37, 0]],
[['E5', 54, 0]],
[['E6', 58, 0]],
[['E7', 85, 0]],
[['E8', 26, 0]],
[['E9', 76, 0]],
[['E10', 85, 0]]],
[[['F1', 38, 0]],
[['F2', 67, 0]],
[['F3', 32, 0]],
[['F4', 2, 0]],
[['F5', 76, 0]],
[['F6', 97, 0]],
[['F7', 34, 0]],
[['F8', 30, 0]],
[['F9', 58, 0]],
[['F10', 73, 0]]],
[[['G1', 68, 0]],
[['G2', 20, 0]],
[['G3', 60, 0]],
[['G4', 46, 0]],
[['G5', 89, 0]],
[['G6', 85, 0]],
[['G7', 76, 0]],
[['G8', 58, 0]],
[['G9', 86, 0]],
[['G10', 49, 0]]],
[[['H1', 22, 0]],
[['H2', 76, 0]],
[['H3', 50, 0]],
[['H4', 2, 0]],
[['H5', 6, 0]],
[['H6', 60, 0]],
[['H7', 92, 0]],
[['H8', 9, 0]],
[['H9', 26, 0]],
[['H10', 91, 0]]],
[[['I1', 57, 0]],
[['I2', 89, 0]],
[['I3', 1, 0]],
[['I4', 7, 0]],
[['I5', 25, 0]],
[['I6', 70, 0]],
[['I7', 40, 0]],
[['I8', 21, 0]],
[['I9', 7, 0]],
[['I10', 93, 0]]],
[[['J1', 77, 0]],
[['J2', 23, 0]],
[['J3', 24, 0]],
[['J4', 48, 0]],
[['J5', 17, 0]],
[['J6', 64, 0]],
[['J7', 11, 0]],
[['J8', 33, 0]],
[['J9', 23, 0]],
[['J10', 20, 0]]]]
如果縮進是一個太深,你只是刪除追加到fullGrid
不會損壞完整的休息。
http://stackoverflow.com/questions/10668341/create-3d-array-using-python你可以做python -v來找到你的Python版本。 – Joe
@Joe事實證明,這是問題。但我無法弄清楚。我打開的格式是[打開整個列表(x10)[打開第二個列表(x10)[打開第三個列表(x3)]]] – user2233549
您如何定義fullGrid(底部的方式)存在問題。如果您告訴它在循環之前僅打印fullGrid [0] [0] [0]會發生什麼?你也會得到列表索引超出範圍。您的列表現在定義爲2x2,而不是3x3。 – Joe