Python Double Indexing Flashcards

 

card 1 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> type(M)
>>> len(M)

 

lists can contain items that are lists

list
3

card 2 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M[1]

 

lists can contain items that are lists

[4, 5, 6, 7]

card 3 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> type(M[2])

 

lists can contain items that are lists

list (it's [8,9])

card 4 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> (M[2])[0]

 

M[2] is a list, so (M[2])[0] makes sense

8

card 5 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> (M[2])[-1]

 

M[2] is a list, so (M[2])[-1] makes sense

9

card 6 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M[2][1]

 

M[2] is a list, so M[2][1] makes sense

9

card 7 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M[1]+M[2]+M[0]

 

the + operator for lists is concatentation

[4, 5, 6, 7, 8, 9, 1, 2, 3]

card 8 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M[1] is B

 

items in lists can be mutable objects

True

card 9 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M[1][0] != B[0]

 

with double indexing like M[1][0], evaluate left to right

False -- M[1][0] is equal to B[0]

card 10 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> len(M[0]) + len(M[1]) + len(M[2])

 

each item of M is a list

9 (it's 3 + 4 + 2)

card 11 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M[0][2] = 7
>>> A

 

M[0] is A (they are aliases of same thing)

[1,2,7]

card 12 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M[0][0] + M[0][1] + M[0][2]

 

M[0] is A (they are aliases of same thing)

7

card 13 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M[0][0] + M[1][0] + M[2][0]

 

each item of M is a non-empty list

13

card 14 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M[0][1] + M[1][1] + M[2][1]

 

each item of M is a non-empty list

16

card 15 of 29

 

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M[0][2] + M[1][2] + M[2][2]

 

each item of M is a non-empty list

IndexError (list of M[2] has only two items)

card 16 of 29

 

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = []
for e in M:
   S = S + e
print(S)

 

each item of M is a non-empty list

[1, 2, 3, 4, 5, 6, 7, 8, 9]

card 17 of 29

 

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = 0
for e in M:
   S = S + e
print(S)

 

each item of M is a non-empty list

Error - can't use + with number and list

card 18 of 29

 

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = 0
for e in M:
   S = S + e[0]
print(S)

 

each item of M is a non-empty list

13 (equals 1 + 4 + 8)

card 19 of 29

 

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = 0
for e in M:
   S = S + e[1]
print(S)

 

each item of M is a non-empty list

16 (equals 2 + 5 + 9)

card 20 of 29

 

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = 1
for e in M:
   S = S * e[1]
print(S)

 

each item of M is a non-empty list

90 (equals 2 * 5 * 9)

card 21 of 29

 

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
P = 1
for e in M:
   P *= e[1]
print(P)

 

each item of M is a non-empty list

90 (equals 2 * 5 * 9)

card 22 of 29

 

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
P = 1
for e in M:
   P *= sum(e)
print(S)

 

each item of M is a non-empty list

918 (equals sum(M[0]) * sum(M[1]) * sum(M[2]) )

card 23 of 29

 

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = 0
for C in M:
  P = 1
  for v in C:
    P = P * v
  S = S + P
print(S)

 

S hints at sum, P hints at product

918 (equals 1*2*3 + 4*5*6*7 + 8*9)

card 24 of 29

 

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = 0
for i in range(len(M)):
  P = 1
  for v in M[i]:
    P = P * v
  S = S + P
print(S)

 

S hints at sum, P hints at product

918 (equals 1*2*3 + 4*5*6*7 + 8*9)

card 25 of 29

 

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = 0
for i in range(len(M)):
  P = 1
  for j in range(len(M[i])):
    P = P * M[i][j]
  S = S + P
print(S)

 

S hints at sum, P hints at product

918 (equals 1*2*3 + 4*5*6*7 + 8*9)

card 26 of 29

 

A = [1,2,3]
B = [4,5,6]
M = [A,B,[7,8,9]]
S = 0
for i in range(len(M)):
  P = 1
  for j in range(len(M[i])):
    P = P * M[j][i]
  S = S + P
print(S)

 

Tricky! Product changes row in each iteration

270 (equals 1*4*7 + 2*5*8 + 3*6*9)

card 27 of 29

 

>>> A = "once"
>>> B = "twice"
>>> M = [A,B,"thrice"]
>>> M[0][2] + M[1][2]

 

each item of M is a non-empty sequence (string)

'ci'

card 28 of 29

 

>>> A = "once"
>>> B = "twice"
>>> M = [A,B,"thrice"]
>>> M[1][1] = 'k'
>>> B

 

each item of M is a non-empty sequence (string)

Error! Strings are immutable

card 29 of 29

 

M = "one four twentyfive thousandmillion".split()
A = ""
for i in range(len(M)):
     A = A + M[i][i:2*i]
print(A)

 

figure out M[0][0:0], then M[1][1:2], then M[2][2:4], ...

oenusa

 

End of Problem Set

(use browser reload to restart this problem set)