我正在尝试创建一个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 :(得分: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],
...
答案 1 :(得分: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
的附加内容,而不会损坏其他完整性。
答案 2 :(得分: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