trying slicing of 2D list having dimensions = (3 X 3), whch results in 4 sub lists of (2 X 2) [ without numpy and other possible libraries ]
lis = [[1,1,1],[2,2,2],[3,3,3]]
print(lis)
dex = list([])
for i in range(0,2):
for j in range(0,2):
dex.append( [ lis[i:i+2,j:j+2] ] )
# print( lis[i:i+2,j:j+2] )
expected:
[ [[1,1][2,2]], [[1,1][2,2]], [[2,2][3,3]], [[2,2][3,3]] ]
getting: TypeError: list indices must be integers or slices, not tuple
Just use list comprehension to get the same effect. For a 3D list, you'd have another comprehension inside that.
Instead of [lis[i:i+2,j:j+2]]
Use [x[j:j+2] for x in lis[i:i+2]]
The output of that is [[[1, 1], [2, 2]], [[1, 1], [2, 2]], [[2, 2], [3, 3]], [[2, 2], [3, 3]]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.