0

I'm trying to create a list of dictionaries in an array format. Initially I would like the first row and column to have

{'score' : 0, 'pointer' : 'none'}

in each cell, however my for loops do not seem to be carrying this out.
Here is what I have so far:

mymatrix = [[0 for x in range(len(seq1)+1)]for x in range(len(seq2)+1)]

mymatrix[0][0] = {'score' : 0, 'pointer' : 'none'}

for x in mymatrix[0][:]:
    x = {'score' : 0, 'pointer' : 'none'}
for y in mymatrix[:][0]:
    y = {'score' : 0, 'pointer' : 'none'}

for row in mymatrix:
    print row

Where seq1 and seq2 are strings.

3
  • what are seq1 and seq2? Commented Jul 8, 2014 at 17:10
  • seq1 and seq2 are strings which are entered using raw_input, I need the number of columns and number of rows to be one more than the length of seq1 and seq2 respectively. Commented Jul 8, 2014 at 17:13
  • show some example input and expected output Commented Jul 8, 2014 at 17:14

2 Answers 2

1

You already wrote two for loops in your list comprehension, you can just reuse them.

(I modified your list comprehension, I used y and x instead of 2 x)

seq1 = 'asdfasdfasdf'
seq2 = 'asdfasdfasdf'

mymatrix = [[0 for y in range(len(seq1)+1)]for x in range(len(seq2)+1)]
#              ^^^^^^^^^^^^^^^^^^^^^^^^^^ reuse this


d = {'score' : 0, 'pointer' : 'none'}

for y in range(len(seq1)+1):
    mymatrix[0][y] = d.copy()

# The first one is already covered, just leave it off using slice
for x in range(len(seq2)+1)[1:]:
    mymatrix[x][0] = d.copy()

for line in mymatrix:
    print(line)

Out:

[{'pointer': 'none', 'score': 0}, {'pointer': 'none', 'score': 0}, {'pointer': 'none', 'score': 0}, {'pointer': 'none', 'score': 0}, {'pointer': 'none', 'score': 0}, {'pointer': 'none', 'score': 0}, {'pointer': 'none', 'score': 0}, {'pointer': 'none', 'score': 0}, {'pointer': 'none', 'score': 0}, {'pointer': 'none', 'score': 0}, {'pointer': 'none', 'score': 0}, {'pointer': 'none', 'score': 0}, {'pointer': 'none', 'score': 0}]
[{'pointer': 'none', 'score': 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[{'pointer': 'none', 'score': 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[{'pointer': 'none', 'score': 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[{'pointer': 'none', 'score': 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[{'pointer': 'none', 'score': 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[{'pointer': 'none', 'score': 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[{'pointer': 'none', 'score': 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[{'pointer': 'none', 'score': 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[{'pointer': 'none', 'score': 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[{'pointer': 'none', 'score': 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[{'pointer': 'none', 'score': 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[{'pointer': 'none', 'score': 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Sign up to request clarification or add additional context in comments.

Comments

0

As far as I can tell, the problem is here:

x = {'score' : 0, 'pointer' : 'none'}

This line of code isn't going to mutate the actual matrix. All you're doing here is reassigning the value of the name x, which is not inherently bound to the matrix you're trying to change. You need to directly reference the matrix. Try this instead:

for xind, x in enumerate(mymatrix[0][:]):
  mymatrix[0][xind] = {'score' : 0, 'pointer' : 'none'}

4 Comments

I get this error: TypeError: list indices must be integers, not tuple
That's a bizarre error. enumerate returns integers specifically for that purpose. Make sure you're using xind and not x.
Definitely using xind and not x. Is enumerate from numpy or something?
Not that I know of, it should be built in. I didn't have to import anything to get it working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.