Python numpy:如果数组元素在给定集合中,则返回布尔值并返回true
要使这项工作成功,需要什么样的规范功能 我尝试过的 我尝试过对lambda函数进行矢量化,这很有效。。。感觉这样做不对Python numpy:如果数组元素在给定集合中,则返回布尔值并返回true,python,numpy,Python,Numpy,要使这项工作成功,需要什么样的规范功能 我尝试过的 我尝试过对lambda函数进行矢量化,这很有效。。。感觉这样做不对 >>> s = {0, 4, 27} >>> a = numpy.arange(10) >>> t = some_func(a) >>> t [1, 0, 0, 0, 1, 0, 0, 0, 0, 0] 与s一起用作NumPy数组 >>> f = lambda i: i in s >
>>> s = {0, 4, 27}
>>> a = numpy.arange(10)
>>> t = some_func(a)
>>> t
[1, 0, 0, 0, 1, 0, 0, 0, 0, 0]
与s
一起用作NumPy数组
>>> f = lambda i: i in s
>>> vf = numpy.vectorize(f)
>>> t = numpy.fromfunction(vf, a.shape)
>>> t.astype(int)
array([1, 0, 0, 0, 1, 0, 0, 0, 0, 0])
与s
一起用作NumPy数组
>>> f = lambda i: i in s
>>> vf = numpy.vectorize(f)
>>> t = numpy.fromfunction(vf, a.shape)
>>> t.astype(int)
array([1, 0, 0, 0, 1, 0, 0, 0, 0, 0])
与s
一起用作NumPy数组
>>> f = lambda i: i in s
>>> vf = numpy.vectorize(f)
>>> t = numpy.fromfunction(vf, a.shape)
>>> t.astype(int)
array([1, 0, 0, 0, 1, 0, 0, 0, 0, 0])
与s
一起用作NumPy数组
>>> f = lambda i: i in s
>>> vf = numpy.vectorize(f)
>>> t = numpy.fromfunction(vf, a.shape)
>>> t.astype(int)
array([1, 0, 0, 0, 1, 0, 0, 0, 0, 0])
这将更快:
>>> import numpy as np
>>> s = np.array([0, 4, 27])
>>> a = np.arange(10)
>>> t = np.in1d(a, s)
>>> t
array([ True, False, False, False, True, False, False, False, False, False], dtype=bool)
>>>
t=np.zero(10,int)
sa=np.数组(列表)
t[sa[sa这会更快:
>>> import numpy as np
>>> s = np.array([0, 4, 27])
>>> a = np.arange(10)
>>> t = np.in1d(a, s)
>>> t
array([ True, False, False, False, True, False, False, False, False, False], dtype=bool)
>>>
t=np.zero(10,int)
sa=np.数组(列表)
t[sa[sa这会更快:
>>> import numpy as np
>>> s = np.array([0, 4, 27])
>>> a = np.arange(10)
>>> t = np.in1d(a, s)
>>> t
array([ True, False, False, False, True, False, False, False, False, False], dtype=bool)
>>>
t=np.zero(10,int)
sa=np.数组(列表)
t[sa[sa这会更快:
>>> import numpy as np
>>> s = np.array([0, 4, 27])
>>> a = np.arange(10)
>>> t = np.in1d(a, s)
>>> t
array([ True, False, False, False, True, False, False, False, False, False], dtype=bool)
>>>
t=np.zero(10,int)
sa=np.数组(列表)
我认为你想要的是来自numpy的一维。不要真的去做numpy
,但是想[i in s for i in a]
在这里工作吗?我想你想要的是来自numpy的一维。不要真的去做numpy
,而是想[i in s for i in a]
在这里工作?我想你想要的是来自numpy的一维。不要真的做numpy
,但是像[I in s for I in a]
这样的东西会在这里工作吗?我想你想要的是来自numpy的一维。不要真的做numpy
,而是像[I in s for I in a]
这样的东西会在这里工作吗?