2013-04-01 84 views
0

我想創建一個3x10x10矩陣,我覺得我已經做對了。我試圖讓GridSet函數爲我設置所有的值,並且我收到第7行的「列表索引超出範圍」的錯誤。我不明白爲什麼我會收到錯誤。創建列表索引超出範圍的矩陣?

如果沒有方括號(沒有模塊,我聽說過NumPy,但我想這樣做的準系統)噸創建矩陣有一個更好的方法,這將是很好的知道。

我正在運行2.6.x,不完全確定哪個版本。任何幫助,將不勝感激。

import random 

def GridSet(fullGrid): 
    letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] 
    for j in range(10): 
     for i in range(10): 
      fullGrid[j][i][0].append(letters[j]+str(i+1)) 
      fullGrid[j][i][1].append(random.randrange(100)+1) 
      fullGrid[j][i][2].append(0) 


#main 
fullGrid = [[[],[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[],[]],[[],[],[],  [],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[],[]]] 
filledGrid = GridSet(fullGrid) 
print filledGrid 
+0

http://stackoverflow.com/questions/10668341/create-3d-array-using-python你可以做python -v來找到你的Python版本。 – Joe

+0

@Joe事實證明,這是問題。但我無法弄清楚。我打開的格式是[打開整個列表(x10)[打開第二個列表(x10)[打開第三個列表(x3)]]] – user2233549

+0

您如何定義fullGrid(底部的方式)存在問題。如果您告訴它在循環之前僅打印fullGrid [0] [0] [0]會發生什麼?你也會得到列表索引超出範圍。您的列表現在定義爲2x2,而不是3x3。 – Joe

回答

1

下嵌套列表理解應該創建原始列表:

In [13]: [[[None for i in range(3)] for j in range(10)] for k in range(10)] 
Out[13]: 
[[[None, None, None], 
    [None, None, None], 
    [None, None, None], 
    [None, None, None], 
    [None, None, None], 
    [None, None, None], 
    [None, None, None], 
    [None, None, None], 
    [None, None, None], 
    [None, None, None]], 
[[None, None, None], 
    [None, None, None], 
    [None, None, None], 
    ... etc 

雖然你可以創建整個事情,而不是首先創建列表,然後填充它的:

In [23]: l = [[[letters[j]+str(i+1), random.randrange(100)+1, 0] for i in range(10)]for j in range(10)] 
In [24]: pprint(l) 
[[['a1', 82, 0], 
    ['a2', 77, 0], 
    ['a3', 64, 0], 
    ['a4', 34, 0], 
    ['a5', 95, 0], 
    ['a6', 69, 0], 
    ['a7', 4, 0], 
    ['a8', 72, 0], 
    ['a9', 83, 0], 
    ['a10', 100, 0]], 
[['b1', 19, 0], 
    ['b2', 59, 0], 
    ['b3', 15, 0], 
    ... 
+0

我在'letter'列表中看到了10(0,9)個字母。我看錯了嗎? ;) – 2013-04-01 20:49:35

+0

@Mariano這是不對的。字母有10個元素。 – Joe

+0

你說得對,我完全讀了別的東西。我的錯。 – Mariano

0

有點困惑。這是你試圖達到的目標嗎?

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不會損壞完整的休息。

0

你需要一個更好的描述,但我只想寫一些簡單的內容來解決我認爲你在尋找的問題。

import random 

def GridSet(letters): 
    letRange = range(len(letters)) 
    return [ [[letter for letter in letters] for v in letRange], 
      [[random.randrange(100)+1 for i in letRange] for v in letRange], 
      [[0 for i in letRange] for v in letRange] ] 

grid = GridSet(["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]) 

# len(grid): 3 
# len(grid[0]): 10 
# len(grid[0][0]): 10