简体   繁体   中英

How does list indexing work?

This question is in python:

battleships = [['0','p','0','s'],
               ['0','p','0','s'],
               ['p','p','0','s'],
               ['0','0','0','0']]
def fun(a,b,bships): 
    c = len(bships)
    return bships[c-b][a-1]

print(fun(1,1,battleships))
print(fun(1,2,battleships))

first print gives 0 second print gives p

I can't work out why, if you could give an explanation it would be much appreciated.

Thank you to those who help :)

Indexing starts at 0 . So battleships contains items at indexes 0 , 1 , 2 , 3 .

First len(bships) gets the length of the list of lists battleships , which is 4.

bships[cb][a-1] accesses items in a list through their index value. So with your first call to the function:

print(fun(1,1,battleships))

It's bships[4-1][1-1] which is bships[3][0] which is ['0','0','0','0'][0] which is 0

You can work it out easily by replacing the calculations with the actual values:

In the first call, you are indexing:

bships[c-b][a-1] == bships[4-1][1-1] == bships[3][0]

Counting from 0, that's the last row, ['0','0','0','0'] , first element, '0' .

The second call evaluates to:

bships[c-b][a-1] == bships[4-2][1-1] == bships[2][0]

so first cell of the second-last row, ['p','p','0','s'] is a 'p' .

Note that in Python, you can use negative indices without calculating the len() first; remove the c from your function and it'll all work just the same:

>>> battleships = [['0','p','0','s'],
...                ['0','p','0','s'],
...                ['p','p','0','s'],
...                ['0','0','0','0']]
>>> def fun(a,b,bships): 
...     return bships[-b][a-1]
... 
>>> print(fun(1,1,battleships))
0
>>> print(fun(1,2,battleships))
p

That is because Python treats negative indices as counting from the end; internally it'll use the length of the sequence (which is stored with the sequence) to calculate just the same thing but faster.

>>> battleships = [['0','p','0','s'],
...                ['0','p','0','s'],
...                ['p','p','0','s'],
...                ['0','0','0','0']]
>>> 
>>> a = 1 
>>> b = 1
>>> c = len(battleships)
>>> c-b,a-1       
(3, 0)

Now battleships[cb][a-1] can be broken into two parts:

battleships[cb] and [a-1]

Python first calls battleships[cb] as cb is 3 so it returns the last list(4th item) from battleships. ie ['0','0','0','0'] (indexing starts at 0 )

Now comes the second part : [a-1]

Now [a-1] is called on that returned list, ie ['0','0','0','0']

['0','0','0','0'][a-1] as a-1 is 0 so python returns the first item from this list.

so you got '0'.

Same is applied for the different values of a , b :

>>> a = 1
>>> b = 2
>>> c-b,a-1
(2, 0)
>>> battleships[c-b]
['p', 'p', '0', 's']
>>> battleships[c-b][a-1]   #calls  ['p', 'p', '0', 's'][0]
'p'

When you are having problem understanding any new thing in programming change the program a bit.

I'll give you an example. I changed it a bit.

battleships = [['0','p','0','s','3'],
               ['0','p','0','s','8'],
               ['p','p','0','s','2']]

print "len(battleships) =",len(battleships)
print "battleships[0] =",battleships[0]
print "battleships[1] =",battleships[1]
print "battleships[2] =",battleships[2]
print "len(battleships[0]) =", len(battleships[0])

When I run it the output is

len(battleships) = 3
battleships[0] = ['0', 'p', '0', 's', '3']
battleships[1] = ['0', 'p', '0', 's', '8']
battleships[2] = ['p', 'p', '0', 's', '2']
len(battleships[0]) = 5

Match the output statements with the print statements. That will help.

Try print battleships[0][1] etc.


One more suggestion. Search google for Python and install it on your computer. If I am correct this is from codecademy. Using only codecademy won't be enough. You will need to write Python scripts, run them and see their outputs. Only after that you'll be able to learn. Try this out. They are better than codecademy at teaching programming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM