gitm
gitm

Reputation: 15

Matrix creation with list index out of range?

I'm trying to create a 3x10x10 matrix, and I feel like I've done it right. I'm trying to get the GridSet function to set all the values for me, and I'm getting an error that is "List index out of range" at line 7. I don't understand why I'm getting the error.

If there's a better way of creating the matrix without having tons of square brackets (without modules, I've heard of NumPy but I want to do this barebones) that would be good to know.

I'm running 2.6.x, not exactly sure which version. Any help would be appreciated.

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

Upvotes: 0

Views: 362

Answers (3)

Michael David Watson
Michael David Watson

Reputation: 3071

You need a better description, but I just want to write what could easily be a one-liner to solve what I think you are looking for.

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

Upvotes: 0

mariano
mariano

Reputation: 1367

The following nested list comprehension should create the original list:

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

Although you could create the entire thing instead of creating the list first and then populating it:

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],
   ...

Upvotes: 1

user1467267
user1467267

Reputation:

A bit confused. Is this what you try to achieve?

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)

Output

[[[['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]]]]

If the indent is one too deep, you just remove an append to the fullGrid without damaging the rest of the integrity.

Upvotes: 0

Related Questions