Jump to content

Hi,

 

I created a 10 by 20 matrix by using this code:

 

[ code ]

field = [["" for x in range(h)] for y in range(w)]

[ /code ]

 

the next thing I try to do is filling the last column with a filledMarker (which equals "x").

 

[ code ]

for i in range(0, 10):

           field(19)(i)= filledMarker

[ /code ]

  *I changed "[]" to "()" to not come in conflict with the forums code syntax

 

but this does not work. Python fills the first column, not the last. If I run this script directly from the terminal it works. But if I run it out of a file it does not. It treats the 19 like a one.

The weird thing is that if I repeat the last code segment, it treats the 19 as a 2, 3, 4, etc...

 

Can anyone help me?

 

Chris

Link to comment
https://linustechtips.com/topic/591805-python-3-cannot-access-lists-correctly/
Share on other sites

Link to post
Share on other sites

As a quick note, don't put spaces inside your code tags--your code won't show up in a code block if you have spaces inside your square backets.

 

As for your actual problem: I can't replicate what you say is happening.  Your code is changing the last row, by the way, assuming that your array is structured like

[ [row1-1, row1-2, ...]
  [row2-1, row2-2, ...] ]

and not

[ [col1-1, col1-2, ...]
  [col2-1, col2-2, ...] ]

 

Here's the code I ran, copied and pasted.  I get the same result in a terminal/IDLE window as when I write a .py file and run it in the command line.

h = 10
w = 20
field = [['' for i in range(h)] for j in range(w)]
for i in range(10):
    field[19][i] = 'x'
print(field)

And the output, in both cases (I put each sub-list on its own line for readability, but otherwise, this is copied and pasted):

[ ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', ''],
  ['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'] ]

Can you give any more details?  Maybe double-check that you typed the right code in to your file?  That's the only thing I can think of at the moment.

Link to post
Share on other sites

Thank you for your quick replies! I fixed the problem by myself already, i did not had the time to go here again unfortunatly... The problem was somewhere else in the code. The empty marker was "" and not " ". Thats the reason why the print funktion did not work and thats the reason why there was such a strange result.

 

Have a nice day! :)

 

Chris

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×