Re: [Numpy-discussion] use slicing as argument values?
Benjamin Root ben.root at ou.edu writes: [clip] a[sl] and a[3:5, 5:14] is equivalent to sl = (slice(3, 5), slice(5, 14)) a[sl] [clip] which is also equivalent to sl = np.s_[3:5, 5:14] ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] use slicing as argument values?
On 12/07/2012 23:32, Chao YUE wrote: Thanks all for the discussion. Actually I am trying to use something like numpy ndarray indexing in the function. Like when I call: func(a,'1:3,:,2:4'), it knows I want to retrieve a[1:3,:,2:4], and func(a,'1:3,:,4') for a[1:3,:,4] ect. I am very close now. I don't see the advantage of this approach over directly using the sliced array as an argument of your function, as in func(a[1:3,:,4]). Can you elaborate more why you are going through this route? Cheers, Daniele ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] use slicing as argument values?
Thanks Daniele. I am writing a small plotting function that can receive the index range as argument value. like I have variables var1, var2, var3, var4, var5 which have exactly the same dimensions. def plot_eg(index_range): #here I need the function above which can use the index_range to retrieve data from variables plot(func(var1,index_range))) plot(func(var2,index_range)) plot(func(var3,index_range)) plot(func(var4,index_range)) plot(func(var5,index_range)) actually I can also put the [var1,var2,var3,var4,var5] as arguments in the plot_eg function so that I can pick any variables I want to plot as long as they have the same dimension. otherwise, I have to change the index_range for every variable. cheers, Chao 2012/7/13 Daniele Nicolodi dani...@grinta.net On 12/07/2012 23:32, Chao YUE wrote: Thanks all for the discussion. Actually I am trying to use something like numpy ndarray indexing in the function. Like when I call: func(a,'1:3,:,2:4'), it knows I want to retrieve a[1:3,:,2:4], and func(a,'1:3,:,4') for a[1:3,:,4] ect. I am very close now. I don't see the advantage of this approach over directly using the sliced array as an argument of your function, as in func(a[1:3,:,4]). Can you elaborate more why you are going through this route? Cheers, Daniele ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion -- *** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16 ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] use slicing as argument values?
On Thu, Jul 12, 2012 at 2:32 PM, Chao YUE chaoyue...@gmail.com wrote: numpy ndarray indexing in the function. Like when I call: func(a,'1:3,:,2:4'), it knows I want to retrieve a[1:3,:,2:4], and func(a,'1:3,:,4') for a[1:3,:,4] ect. why do the string packing/unpacking? why not use an interface much like the slice() and range() functions? func(a, ( (start, stop, step),(start, stop, step),(sart, stop, step) )) or, I agree, jsut pass in the sliced array: func( a[start:stop:step, start:stop:step, start_stop:step] ) Will the rank of a always be 3? Do you ned to support step that could simplify it a bit. -Chris I am very close now. #so this function changes the string to list of slice objects. def convert_string_to_slice(slice_string): provide slice_string as '2:3,:', it will return [slice(2, 3, None), slice(None, None, None)] slice_list=[] split_slice_string_list=slice_string.split(',') for sub_slice_string in split_slice_string_list: split_sub=sub_slice_string.split(':') if len(split_sub)==1: sub_slice=slice(int(split_sub[0])) else: if split_sub[0]=='': sub1=None else: sub1=int(split_sub[0]) if split_sub[1]=='': sub2=None else: sub2=int(split_sub[1]) sub_slice=slice(sub1,sub2) slice_list.append(sub_slice) return slice_list In [119]: a=np.arange(3*4*5).reshape(3,4,5) for this it works fine. In [120]: convert_string_to_slice('1:3,:,2:4') Out[120]: [slice(1, 3, None), slice(None, None, None), slice(2, 4, None)] In [121]: a[slice(1, 3, None), slice(None, None, None), slice(2, 4, None)]==a[1:3,:,2:4] Out[121]: array([[[ True, True], [ True, True], [ True, True], [ True, True]], [[ True, True], [ True, True], [ True, True], [ True, True]]], dtype=bool) And problems happens when I want to retrieve a single number along a given dimension: because it treats 1:3,:,4 as 1:3,:,:4, as shown below: In [122]: convert_string_to_slice('1:3,:,4') Out[122]: [slice(1, 3, None), slice(None, None, None), slice(None, 4, None)] In [123]: a[1:3,:,4] Out[123]: array([[24, 29, 34, 39], [44, 49, 54, 59]]) In [124]: a[slice(1, 3, None), slice(None, None, None), slice(None, 4, None)] Out[124]: array([[[20, 21, 22, 23], [25, 26, 27, 28], [30, 31, 32, 33], [35, 36, 37, 38]], [[40, 41, 42, 43], [45, 46, 47, 48], [50, 51, 52, 53], [55, 56, 57, 58]]]) Then I have a function: #this function retrieves data from ndarray a by specifying slice_string: def retrieve_data(a,slice_string): slice_list=convert_string_to_slice(slice_string) return a[*slice_list] In the list line of the fuction retrieve_data I have problem, I get an invalid syntax error. return a[*slice_list] ^ SyntaxError: invalid syntax I hope it's not too long, please comment as you like. Thanks a lot Chao 2012/7/12 Benjamin Root ben.r...@ou.edu On Thu, Jul 12, 2012 at 4:46 PM, Chao YUE chaoyue...@gmail.com wrote: Hi Ben, it helps a lot. I am nearly finishing a function in a way I think pythonic. Just one more question, I have: In [24]: b=np.arange(1,11) In [25]: b Out[25]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) In [26]: b[slice(1)] Out[26]: array([1]) In [27]: b[slice(4)] Out[27]: array([1, 2, 3, 4]) In [28]: b[slice(None,4)] Out[28]: array([1, 2, 3, 4]) so slice(4) is actually slice(None,4), how can I exactly want retrieve a[4] using slice object? thanks again! Chao Tricky question. Note the difference between a[4] and a[4:5] The first returns a scalar, while the second returns an array. The first, though, is not a slice, just an integer. Also, note that the arguments for slice() behaves very similar to the arguments for range() (with some exceptions/differences). Cheers! Ben Root 2012/7/12 Benjamin Root ben.r...@ou.edu On Thu, Jul 12, 2012 at 3:38 PM, Chao YUE chaoyue...@gmail.com wrote: Dear all, I want to create a function and I would like one of the arguments of the function to determine what slicing of numpy array I want to use. a simple example: a=np.arange(100).reshape(10,10) suppose I want to have a imaging function to show image of part of this data: def show_part_of_data(m,n): plt.imshow(a[m,n]) like I can give m=3:5, n=2:7, when I call function show_part_of_data(3:5,2:7), this means I try to do plt.imshow(a[3:5,2:7]). the above example doesn't work in reality. but it illustrates something similar that I desire, that is, I can specify what slicing of number array I want by giving values to function arguments. thanks a lot, Chao What you want to do is create slice objects. a[3:5] is equivalent to sl = slice(3, 5) a[sl]
Re: [Numpy-discussion] use slicing as argument values?
On Thu, Jul 12, 2012 at 10:32 PM, Chao YUE chaoyue...@gmail.com wrote: Thanks all for the discussion. Actually I am trying to use something like numpy ndarray indexing in the function. Like when I call: func(a,'1:3,:,2:4'), it knows I want to retrieve a[1:3,:,2:4], and func(a,'1:3,:,4') for a[1:3,:,4] ect. I am very close now. [~] |1 from numpy import index_exp [~] |2 index_exp[1:3,:,2:4] (slice(1, 3, None), slice(None, None, None), slice(2, 4, None)) -- Robert Kern ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] use slicing as argument values?
Thanks Robert. This is exactly what I want. I have a feeling that there must be something in numpy that can do the job and I didn't know. Thanks again, Chao 2012/7/13 Robert Kern robert.k...@gmail.com On Thu, Jul 12, 2012 at 10:32 PM, Chao YUE chaoyue...@gmail.com wrote: Thanks all for the discussion. Actually I am trying to use something like numpy ndarray indexing in the function. Like when I call: func(a,'1:3,:,2:4'), it knows I want to retrieve a[1:3,:,2:4], and func(a,'1:3,:,4') for a[1:3,:,4] ect. I am very close now. [~] |1 from numpy import index_exp [~] |2 index_exp[1:3,:,2:4] (slice(1, 3, None), slice(None, None, None), slice(2, 4, None)) -- Robert Kern ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion -- *** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16 ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] use slicing as argument values?
On Fri, Jul 13, 2012 at 9:24 AM, Robert Kern robert.k...@gmail.com wrote: On Thu, Jul 12, 2012 at 10:32 PM, Chao YUE chaoyue...@gmail.com wrote: Thanks all for the discussion. Actually I am trying to use something like numpy ndarray indexing in the function. Like when I call: func(a,'1:3,:,2:4'), it knows I want to retrieve a[1:3,:,2:4], and func(a,'1:3,:,4') for a[1:3,:,4] ect. I am very close now. [~] |1 from numpy import index_exp [~] |2 index_exp[1:3,:,2:4] (slice(1, 3, None), slice(None, None, None), slice(2, 4, None)) Nice - thanks for the pointer, Matthew ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] use slicing as argument values?
On Thu, Jul 12, 2012 at 3:38 PM, Chao YUE chaoyue...@gmail.com wrote: Dear all, I want to create a function and I would like one of the arguments of the function to determine what slicing of numpy array I want to use. a simple example: a=np.arange(100).reshape(10,10) suppose I want to have a imaging function to show image of part of this data: def show_part_of_data(m,n): plt.imshow(a[m,n]) like I can give m=3:5, n=2:7, when I call function show_part_of_data(3:5,2:7), this means I try to do plt.imshow(a[3:5,2:7]). the above example doesn't work in reality. but it illustrates something similar that I desire, that is, I can specify what slicing of number array I want by giving values to function arguments. thanks a lot, Chao What you want to do is create slice objects. a[3:5] is equivalent to sl = slice(3, 5) a[sl] and a[3:5, 5:14] is equivalent to sl = (slice(3, 5), slice(5, 14)) a[sl] Furthermore, notation such as ::-1 is equivalent to slice(None, None, -1) I hope this helps! Ben Root ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] use slicing as argument values?
Hi Ben, it helps a lot. I am nearly finishing a function in a way I think pythonic. Just one more question, I have: In [24]: b=np.arange(1,11) In [25]: b Out[25]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) In [26]: b[slice(1)] Out[26]: array([1]) In [27]: b[slice(4)] Out[27]: array([1, 2, 3, 4]) In [28]: b[slice(None,4)] Out[28]: array([1, 2, 3, 4]) so slice(4) is actually slice(None,4), how can I exactly want retrieve a[4] using slice object? thanks again! Chao 2012/7/12 Benjamin Root ben.r...@ou.edu On Thu, Jul 12, 2012 at 3:38 PM, Chao YUE chaoyue...@gmail.com wrote: Dear all, I want to create a function and I would like one of the arguments of the function to determine what slicing of numpy array I want to use. a simple example: a=np.arange(100).reshape(10,10) suppose I want to have a imaging function to show image of part of this data: def show_part_of_data(m,n): plt.imshow(a[m,n]) like I can give m=3:5, n=2:7, when I call function show_part_of_data(3:5,2:7), this means I try to do plt.imshow(a[3:5,2:7]). the above example doesn't work in reality. but it illustrates something similar that I desire, that is, I can specify what slicing of number array I want by giving values to function arguments. thanks a lot, Chao What you want to do is create slice objects. a[3:5] is equivalent to sl = slice(3, 5) a[sl] and a[3:5, 5:14] is equivalent to sl = (slice(3, 5), slice(5, 14)) a[sl] Furthermore, notation such as ::-1 is equivalent to slice(None, None, -1) I hope this helps! Ben Root ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion -- *** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16 ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] use slicing as argument values?
On Thu, Jul 12, 2012 at 9:46 PM, Chao YUE chaoyue...@gmail.com wrote: Hi Ben, it helps a lot. I am nearly finishing a function in a way I think pythonic. Just one more question, I have: In [24]: b=np.arange(1,11) In [25]: b Out[25]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) In [26]: b[slice(1)] Out[26]: array([1]) In [27]: b[slice(4)] Out[27]: array([1, 2, 3, 4]) In [28]: b[slice(None,4)] Out[28]: array([1, 2, 3, 4]) so slice(4) is actually slice(None,4), how can I exactly want retrieve a[4] using slice object? You don't. You use 4. -- Robert Kern ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] use slicing as argument values?
On 07/12/2012 04:46 PM, Chao YUE wrote: Hi Ben, it helps a lot. I am nearly finishing a function in a way I think pythonic. Just one more question, I have: In [24]: b=np.arange(1,11) In [25]: b Out[25]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) In [26]: b[slice(1)] Out[26]: array([1]) In [27]: b[slice(4)] Out[27]: array([1, 2, 3, 4]) In [28]: b[slice(None,4)] Out[28]: array([1, 2, 3, 4]) so slice(4) is actually slice(None,4), how can I exactly want retrieve a[4] using slice object? thanks again! Chao slice is a build in python function and the online docs explain its use (http://docs.python.org/library/functions.html#slice). b[slice(4,5)] will give you something close to b[4], but not quite the same. In [8]: b[4] Out[8]: 5 In [9]: b[slice(4,5)] Out[9]: array([5]) - Jonathan Helmus ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] use slicing as argument values?
On Thu, Jul 12, 2012 at 4:46 PM, Chao YUE chaoyue...@gmail.com wrote: Hi Ben, it helps a lot. I am nearly finishing a function in a way I think pythonic. Just one more question, I have: In [24]: b=np.arange(1,11) In [25]: b Out[25]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) In [26]: b[slice(1)] Out[26]: array([1]) In [27]: b[slice(4)] Out[27]: array([1, 2, 3, 4]) In [28]: b[slice(None,4)] Out[28]: array([1, 2, 3, 4]) so slice(4) is actually slice(None,4), how can I exactly want retrieve a[4] using slice object? thanks again! Chao Tricky question. Note the difference between a[4] and a[4:5] The first returns a scalar, while the second returns an array. The first, though, is not a slice, just an integer. Also, note that the arguments for slice() behaves very similar to the arguments for range() (with some exceptions/differences). Cheers! Ben Root 2012/7/12 Benjamin Root ben.r...@ou.edu On Thu, Jul 12, 2012 at 3:38 PM, Chao YUE chaoyue...@gmail.com wrote: Dear all, I want to create a function and I would like one of the arguments of the function to determine what slicing of numpy array I want to use. a simple example: a=np.arange(100).reshape(10,10) suppose I want to have a imaging function to show image of part of this data: def show_part_of_data(m,n): plt.imshow(a[m,n]) like I can give m=3:5, n=2:7, when I call function show_part_of_data(3:5,2:7), this means I try to do plt.imshow(a[3:5,2:7]). the above example doesn't work in reality. but it illustrates something similar that I desire, that is, I can specify what slicing of number array I want by giving values to function arguments. thanks a lot, Chao What you want to do is create slice objects. a[3:5] is equivalent to sl = slice(3, 5) a[sl] and a[3:5, 5:14] is equivalent to sl = (slice(3, 5), slice(5, 14)) a[sl] Furthermore, notation such as ::-1 is equivalent to slice(None, None, -1) I hope this helps! Ben Root ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion -- *** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16 ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] use slicing as argument values?
Thanks all for the discussion. Actually I am trying to use something like numpy ndarray indexing in the function. Like when I call: func(a,'1:3,:,2:4'), it knows I want to retrieve a[1:3,:,2:4], and func(a,'1:3,:,4') for a[1:3,:,4] ect. I am very close now. #so this function changes the string to list of slice objects. def convert_string_to_slice(slice_string): provide slice_string as '2:3,:', it will return [slice(2, 3, None), slice(None, None, None)] slice_list=[] split_slice_string_list=slice_string.split(',') for sub_slice_string in split_slice_string_list: split_sub=sub_slice_string.split(':') if len(split_sub)==1: sub_slice=slice(int(split_sub[0])) else: if split_sub[0]=='': sub1=None else: sub1=int(split_sub[0]) if split_sub[1]=='': sub2=None else: sub2=int(split_sub[1]) sub_slice=slice(sub1,sub2) slice_list.append(sub_slice) return slice_list In [119]: a=np.arange(3*4*5).reshape(3,4,5) for this it works fine. In [120]: convert_string_to_slice('1:3,:,2:4') Out[120]: [slice(1, 3, None), slice(None, None, None), slice(2, 4, None)] In [121]: a[slice(1, 3, None), slice(None, None, None), slice(2, 4, None)]==a[1:3,:,2:4] Out[121]: array([[[ True, True], [ True, True], [ True, True], [ True, True]], [[ True, True], [ True, True], [ True, True], [ True, True]]], dtype=bool) And problems happens when I want to retrieve a single number along a given dimension: because it treats 1:3,:,4 as 1:3,:,:4, as shown below: In [122]: convert_string_to_slice('1:3,:,4') Out[122]: [slice(1, 3, None), slice(None, None, None), slice(None, 4, None)] In [123]: a[1:3,:,4] Out[123]: array([[24, 29, 34, 39], [44, 49, 54, 59]]) In [124]: a[slice(1, 3, None), slice(None, None, None), slice(None, 4, None)] Out[124]: array([[[20, 21, 22, 23], [25, 26, 27, 28], [30, 31, 32, 33], [35, 36, 37, 38]], [[40, 41, 42, 43], [45, 46, 47, 48], [50, 51, 52, 53], [55, 56, 57, 58]]]) Then I have a function: #this function retrieves data from ndarray a by specifying slice_string: def retrieve_data(a,slice_string): slice_list=convert_string_to_slice(slice_string) return a[*slice_list] In the list line of the fuction retrieve_data I have problem, I get an invalid syntax error. return a[*slice_list] ^ SyntaxError: invalid syntax I hope it's not too long, please comment as you like. Thanks a lot Chao 2012/7/12 Benjamin Root ben.r...@ou.edu On Thu, Jul 12, 2012 at 4:46 PM, Chao YUE chaoyue...@gmail.com wrote: Hi Ben, it helps a lot. I am nearly finishing a function in a way I think pythonic. Just one more question, I have: In [24]: b=np.arange(1,11) In [25]: b Out[25]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) In [26]: b[slice(1)] Out[26]: array([1]) In [27]: b[slice(4)] Out[27]: array([1, 2, 3, 4]) In [28]: b[slice(None,4)] Out[28]: array([1, 2, 3, 4]) so slice(4) is actually slice(None,4), how can I exactly want retrieve a[4] using slice object? thanks again! Chao Tricky question. Note the difference between a[4] and a[4:5] The first returns a scalar, while the second returns an array. The first, though, is not a slice, just an integer. Also, note that the arguments for slice() behaves very similar to the arguments for range() (with some exceptions/differences). Cheers! Ben Root 2012/7/12 Benjamin Root ben.r...@ou.edu On Thu, Jul 12, 2012 at 3:38 PM, Chao YUE chaoyue...@gmail.com wrote: Dear all, I want to create a function and I would like one of the arguments of the function to determine what slicing of numpy array I want to use. a simple example: a=np.arange(100).reshape(10,10) suppose I want to have a imaging function to show image of part of this data: def show_part_of_data(m,n): plt.imshow(a[m,n]) like I can give m=3:5, n=2:7, when I call function show_part_of_data(3:5,2:7), this means I try to do plt.imshow(a[3:5,2:7]). the above example doesn't work in reality. but it illustrates something similar that I desire, that is, I can specify what slicing of number array I want by giving values to function arguments. thanks a lot, Chao What you want to do is create slice objects. a[3:5] is equivalent to sl = slice(3, 5) a[sl] and a[3:5, 5:14] is equivalent to sl = (slice(3, 5), slice(5, 14)) a[sl] Furthermore, notation such as ::-1 is equivalent to slice(None, None, -1) I hope this helps! Ben Root ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion -- *** Chao YUE
Re: [Numpy-discussion] use slicing as argument values?
On Thursday, July 12, 2012, Chao YUE wrote: Thanks all for the discussion. Actually I am trying to use something like numpy ndarray indexing in the function. Like when I call: func(a,'1:3,:,2:4'), it knows I want to retrieve a[1:3,:,2:4], and func(a,'1:3,:,4') for a[1:3,:,4] ect. I am very close now. #so this function changes the string to list of slice objects. def convert_string_to_slice(slice_string): provide slice_string as '2:3,:', it will return [slice(2, 3, None), slice(None, None, None)] slice_list=[] split_slice_string_list=slice_string.split(',') for sub_slice_string in split_slice_string_list: split_sub=sub_slice_string.split(':') if len(split_sub)==1: sub_slice=slice(int(split_sub[0])) else: if split_sub[0]=='': sub1=None else: sub1=int(split_sub[0]) if split_sub[1]=='': sub2=None else: sub2=int(split_sub[1]) sub_slice=slice(sub1,sub2) slice_list.append(sub_slice) return slice_list In [119]: a=np.arange(3*4*5).reshape(3,4,5) for this it works fine. In [120]: convert_string_to_slice('1:3,:,2:4') Out[120]: [slice(1, 3, None), slice(None, None, None), slice(2, 4, None)] In [121]: a[slice(1, 3, None), slice(None, None, None), slice(2, 4, None)]==a[1:3,:,2:4] Out[121]: array([[[ True, True], [ True, True], [ True, True], [ True, True]], [[ True, True], [ True, True], [ True, True], [ True, True]]], dtype=bool) And problems happens when I want to retrieve a single number along a given dimension: because it treats 1:3,:,4 as 1:3,:,:4, as shown below: In [122]: convert_string_to_slice('1:3,:,4') Out[122]: [slice(1, 3, None), slice(None, None, None), slice(None, 4, None)] In [123]: a[1:3,:,4] Out[123]: array([[24, 29, 34, 39], [44, 49, 54, 59]]) In [124]: a[slice(1, 3, None), slice(None, None, None), slice(None, 4, None)] Out[124]: array([[[20, 21, 22, 23], [25, 26, 27, 28], [30, 31, 32, 33], [35, 36, 37, 38]], [[40, 41, 42, 43], [45, 46, 47, 48], [50, 51, 52, 53], [55, 56, 57, 58]]]) Then I have a function: #this function retrieves data from ndarray a by specifying slice_string: def retrieve_data(a,slice_string): slice_list=convert_string_to_slice(slice_string) return a[*slice_list] In the list line of the fuction retrieve_data I have problem, I get an invalid syntax error. return a[*slice_list] ^ SyntaxError: invalid syntax I hope it's not too long, please comment as you like. Thanks a lot Chao I won't comment on the wisdom of your approach, but for you very last part, don't try unpacking the slice list. Also, I think it has to be a tuple, but I could be wrong on that. Ben Root ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion