1

I am currently trying to replace elements in a 2D list with elements in another list, so as to implement a game I am making in python. Here is what I have so far:

listA = [1, 3, 5]
listB = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

for a in range(len(listA)):
    alpha = (a/3) #number of rows in listB
    beta = (a/3) #number of columns in listB
    listB[alpha][beta] = 123

When I do this, I get

[[123, 123, 123], [0, 0, 0], [0, 0, 0]] 

instead of what I want given the parameters,

[[0, 123, 0], [123, 0, 123], [0, 0, 0]]

Am I doing something wrong?

10
  • 3
    What do you expect (a/3) to be? It'll never be anything else but 0.. Commented Mar 5, 2013 at 15:33
  • Looping by index is always a bad idea in Python, instead, loop through and build up a new list - using a list comprehension would be a good idea. Commented Mar 5, 2013 at 15:34
  • Actually, it'll only ever be zero...so your output is not possible! Commented Mar 5, 2013 at 15:34
  • 2
    so: 1,3,5 are the indexes where you want that 123 to be put in, and then you want the resulting listB to be split a list of lists? is that it? Commented Mar 5, 2013 at 15:37
  • 1
    alpha = a/3; beta = a%3 Commented Mar 5, 2013 at 15:41

4 Answers 4

3

Instead of iterating through the indices of listA using for a in range(len(listA)):, you should iterate through the elements of listA using for a in listA:

Assuming that the indices in A translate into coordinates in B like so:

0 1 2
3 4 5
6 7 8

Then beta, AKA the column of B corresponding to a, should be calculated as a%3, rather than a/3.

listA = [1, 3, 5]
listB = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

for a in listA:
    #two slashes is integer division, not a comment, 
    #despite markup's color scheme
    alpha = a//3 
    beta = a%3
    listB[alpha][beta] = 123

print listB

Output:

[[0, 123, 0], [123, 0, 123], [0, 0, 0]]
Sign up to request clarification or add additional context in comments.

1 Comment

I tried @Kevin 's suggestion in idle and it worked, thank you Kevin and everyone else who gave input!
3

If you use numpy this is pretty easy:

import numpy as np

A = np.array([1,3,5])
B = np.zeros((3,3))

B.flat[A] = 123

print B

out:

[[   0.  123.    0.]
 [ 123.    0.  123.]
 [   0.    0.    0.]]

Note that what .flat does is return a "flattened" version of your list:

[   0.  123.    0.  123.    0.  123.    0.    0.    0.]

1 Comment

Simpler even: B.flat[A] = 123.
1
>>> for a in range(len(listA)):
...     alpha = (listA[a]/3) #number of rows in listB
...     beta = (listA[a]%3) #number of columns in listB
...     listB[alpha][beta] = 123
... 
>>> listB
[[0, 123, 0], [123, 0, 123], [0, 0, 0]]

you must use the elements inside your listA, or it's pointles to use the indexes generated by range. Also, you should do a bit of math to properly fetch the row and column index

edit: I suggest you take a look at Kevin's answer and explanation, mine is just a quick correction of your code.

2 Comments

Better to iterate through listA than using range and then indexing listA, as in Kevin's answer.
yeah, was just correcting his initial code, Kevin's more pythonic and clear indeed
0
>>> listA = [1, 3, 5]
>>> listB = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> listB_unwrapped = list(chain(*listB))
>>> for i in listA:
    listB_unwrapped[i] = 123


>>> listB = zip(*[iter(listB_unwrapped)]*3)
>>> listB
[(0, 123, 0), (123, 0, 123), (0, 0, 0)]
>>> 

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.