+ 1
If have a two dimension matrix (list), like lists[a,b,c,d][e,f,g]
can we still use list slice? like list[1:3][0:2]
5 odpowiedzi
+ 4
What you created is actually a list of two lists:
matrix = [[a, b, c, d], [e, f, g]]
You can address them separately:
matrix[0][0:2] = [a, b]
+ 3
list[0][0:3]+list[1][0:2]
+ 3
But you can easily define a function to do that stuff:
a = [ [ 1, 2, 3, 4 ] , [ 5, 6, 7, 8 ] , [ 9, 10, 11, 12 ] , [ 13, 14, 15, 16 ] ]
def nested_slice( arr, i1, j1, i2, j2 ):
r = [ ]
for i in range ( i1, j1 ):
r.append(arr[ i ][ i2 : j2 ])
return r
print( nested_slice( a, 1, 3, 1, 3 ) )
... will output: [ [ 6, 7 ] , [ 10, 11 ] ]
+ 1
so how can i extract a new sub-list base origin list, like [[a,b,c],[e,f]]
+ 1
thx @Kuba