+ 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]

10th Feb 2017, 6:02 AM
karlxu
karlxu - avatar
5 Respostas
+ 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]
10th Feb 2017, 7:27 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
list[0][0:3]+list[1][0:2]
10th Feb 2017, 7:59 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 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 ] ]
10th Feb 2017, 11:37 AM
visph
visph - avatar
+ 1
so how can i extract a new sub-list base origin list, like [[a,b,c],[e,f]]
10th Feb 2017, 7:33 AM
karlxu
karlxu - avatar
+ 1
thx @Kuba
10th Feb 2017, 8:02 AM
karlxu
karlxu - avatar