Using : For Multiple Slicing In List Or Numpy Array
I'm having some difficulty trying to figure out how to do extract multiple values in a list that are spaced some indices apart. For example, given a list l = [0,1,2,3,4,5,6,7,8,9,1
Solution 1:
I think you need numpy.r_
for concanecate indices:
print (np.r_[1:4, 6:10])
[1 2 3 6 7 8 9]
Solution 2:
Using list comprehension, you can do:
>>> [item for item in l[1:4] + l[6:-1]]
[1, 2, 3, 6, 7, 8, 9]
You can also use extend()
like this:
>>> res = l[1:4]
>>> res.extend(l[6:-1])
>>> res
[1, 2, 3, 6, 7, 8, 9]
Post a Comment for "Using : For Multiple Slicing In List Or Numpy Array"