0

How to do a SIMPLE subtraction w/ Python in arrays with 3 variables.

a = [[8,8,1] , [6,4,1]]
b = a[0][1] - a [1] [0]
c = a[1:b]
print (c)

Is this the answer as it is simply outputting the 2nd array from a?

Simply typing out the array according to instruction but not understanding how a[1:b]=[6,4,1]? Is 1 = to the 2nd array of 6,4,1 or is it indicating the 2nd array in a? For the b variable is the subtraction just keeping the first line of a = 8,8,1 , 6,4,1 the same so that [1:b] = the 2nd index of a?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • In Python those are lists, not arrays. – Barmar Oct 01 '19 at 09:27
  • 1
    What are `b` and `c`? Variable names are case-sensitive in Python. – Barmar Oct 01 '19 at 09:27
  • Maybe you should read about [accessing items in lists of lists](https://stackoverflow.com/questions/18449360/access-item-in-a-list-of-lists) and also about (list) slicing. – Mike Scotty Oct 01 '19 at 09:29
  • You're not subtracting arrays, you're subtracting numbers. The numbers happened to come from lists, but that's irrelevant to how the subtraction works. – Barmar Oct 01 '19 at 09:30

1 Answers1

2

In

b = a[0][1] – a[1][0]

the value of a[0][1] is 8, and a[1][0] is 6, so this is equivalent to

b = 8 - 2

and sets b to 2.

Then you do

c = a[1:b]

which is equivalent to

c = a[1:2]

This is a slice of the list a starting from index 1 and ending at index 1. Since indexes start at 0, index 1 is the second element, which is [6,4,1]. So the result of this is

c = [[6,4,1]]

See Understanding slice notation for more details about how list slicing works in Python.

Barmar
  • 741,623
  • 53
  • 500
  • 612