将数组范围作为参数传递给函数?

时间:2021-09-17 18:47:59

is there a way to pass array-range as argument to function ? Something like :

有没有办法将数组范围作为参数传递给函数?就像是 :

> blah(ary,arg1=1:5)

def blah(ary,arg1): print ary[arg1]

3 个解决方案

#1


5  

Python accepts the 1:5 syntax only within square brackets. The interpreter converts it into a slice object. The __getitem__ method of the object then applies the slice.

Python仅在方括号内接受1:5语法。解释器将其转换为切片对象。然后,对象的__getitem__方法应用切片。

Look at numpy/lib/index_tricks.py for some functions that take advantage of this. Actually they aren't functions, but rather classes that define their own __getitem__ methods. That file may give you ideas.

查看numpy / lib / index_tricks.py以获取利用此功能的一些函数。实际上它们不是函数,而是定义自己的__getitem__方法的类。该文件可能会给你一些想法。

But if you aren't up to that, then possibilities include:

但如果你不能做到这一点,那么可能包括:

blah(arr, slice(1, 5))
blah(arr, np.r_[1:5])

nd_grid, mgrid, ogrid extend the 'slice' concept to accept an imaginary 'step' value:

nd_grid,mgrid,ogrid扩展'slice'概念以接受虚构的'step'值:

mgrid[-1:1:5j]
# array([-1. , -0.5,  0. ,  0.5,  1. ])

Just be aware that anything which expands on a slice before passing it to your blah function, won't know about the shape of the other argument. So np.r_[:-1] just returns [].

请注意,在将其传递给您的blah函数之前在切片上展开的任何内容都不会知道另一个参数的形状。所以np.r _ [: - 1]只返回[]。

And None can be used in slice: e.g. slice(None,None,-1) is equivalent of [::-1].

并且可以在切片中使用无:例如slice(None,None,-1)相当于[:: - 1]。

#2


4  

You can use the slice function

您可以使用切片功能

>>> def blah(ary,arg1):
...     print ary[arg1]
>>> blah(range(10), slice(1, 5))
[1, 2, 3, 4]

#3


0  

you can try like this:

你可以尝试这样:

def blah(ary, arg):
    arg = map(int, arg.split(":"))
    print ary[arg[0]:arg[1]]

blah([1,2,3,4,5,6],"2:5")

output:

[3, 4, 5]

#1


5  

Python accepts the 1:5 syntax only within square brackets. The interpreter converts it into a slice object. The __getitem__ method of the object then applies the slice.

Python仅在方括号内接受1:5语法。解释器将其转换为切片对象。然后,对象的__getitem__方法应用切片。

Look at numpy/lib/index_tricks.py for some functions that take advantage of this. Actually they aren't functions, but rather classes that define their own __getitem__ methods. That file may give you ideas.

查看numpy / lib / index_tricks.py以获取利用此功能的一些函数。实际上它们不是函数,而是定义自己的__getitem__方法的类。该文件可能会给你一些想法。

But if you aren't up to that, then possibilities include:

但如果你不能做到这一点,那么可能包括:

blah(arr, slice(1, 5))
blah(arr, np.r_[1:5])

nd_grid, mgrid, ogrid extend the 'slice' concept to accept an imaginary 'step' value:

nd_grid,mgrid,ogrid扩展'slice'概念以接受虚构的'step'值:

mgrid[-1:1:5j]
# array([-1. , -0.5,  0. ,  0.5,  1. ])

Just be aware that anything which expands on a slice before passing it to your blah function, won't know about the shape of the other argument. So np.r_[:-1] just returns [].

请注意,在将其传递给您的blah函数之前在切片上展开的任何内容都不会知道另一个参数的形状。所以np.r _ [: - 1]只返回[]。

And None can be used in slice: e.g. slice(None,None,-1) is equivalent of [::-1].

并且可以在切片中使用无:例如slice(None,None,-1)相当于[:: - 1]。

#2


4  

You can use the slice function

您可以使用切片功能

>>> def blah(ary,arg1):
...     print ary[arg1]
>>> blah(range(10), slice(1, 5))
[1, 2, 3, 4]

#3


0  

you can try like this:

你可以尝试这样:

def blah(ary, arg):
    arg = map(int, arg.split(":"))
    print ary[arg[0]:arg[1]]

blah([1,2,3,4,5,6],"2:5")

output:

[3, 4, 5]