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[cb][a-1] print(fun(1,1,battleships)) print(fun(1,2,battleships)) 

first print gives 0 second print gives p

I cannot understand why, if you could give an explanation, it would be very helpful.

Thanks to those who help :)

+4
source share
4 answers

Indexing starts at 0 . Thus, battleships contain elements with indices 0 , 1 , 2 , 3 .

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

bships[cb][a-1] refers to the items in the list through their index value. Therefore, when you first call the function:

 print(fun(1,1,battleships)) 

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

+3
source

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

In the first call, you index:

 bships[cb][a-1] == bships[4-1][1-1] == bships[3][0] 

Count from 0 that the last line, ['0','0','0','0'] , the first element, '0' .

The second call is evaluated as:

 bships[cb][a-1] == bships[4-2][1-1] == bships[2][0] 

therefore, the first cell of the second row, ['p','p','0','s'] is 'p' .

Note that in Python you can use negative indices without first evaluating len() ; remove c from your function and everything will work 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 

This is because Python processes negative indices from the end; internally, it will use the length of the sequence (which is stored in the sequence) to calculate the same thing, but faster.

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

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

battleships[cb] and [a-1]

First, Python calls battleships[cb] as cb is 3 so that it returns the last list (4th element) from the ships of the line. ie ['0','0','0','0'] (indexing starts at 0 )

Now comes the second part: [a-1]

Now [a-1] is called into this returned list, i.e. ['0','0','0','0']

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

so you get "0".

The same applies for different values ​​of a , b :

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

If you have a problem understanding any new thing in programming, change the program a bit.

I will 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 launched it, the output will be

 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 output statements with print operations. This will help.

Try print battleships[0][1] etc.


Another suggestion. Search Google for Python and install it on your computer. If I'm right, this is from codecademy. Using codecademy alone is not enough. You will need to write Python scripts, run them and see their results. Only then can you learn. Try this one . They are better than codecademy in teaching programming.

+1
source

Source: https://habr.com/ru/post/1487703/


All Articles