使用python列表理解根据条件查找元素的索引 [英] Finding the index of elements based on a condition using python list comprehension
问题描述
当来自 Matlab 背景时,以下 Python 代码似乎很冗长
<预><代码>>>>a = [1, 2, 3, 1, 2, 3]>>>[索引的索引,枚举中的值(a)如果值>2][2, 5]在 Matlab 中我可以写:
<代码>>>a = [1, 2, 3, 1, 2, 3];>>找到(a>2)答案 =3 6
是否有用 Python 编写此代码的速记方法,还是我只使用长版本?
<小时>感谢您对 Python 语法原理的所有建议和解释.
在numpy网站上找到以下内容后,我想我找到了一个我喜欢的解决方案:
http://docs.scipy.org/doc/numpy/user/basics.indexing.html#boolean-or-mask-index-arrays
将来自该网站的信息应用于我上面的问题,将给出以下内容:
<预><代码>>>>从 numpy 导入数组>>>a = 数组([1, 2, 3, 1, 2, 3])>>>b=a>2数组([假,假,真,假,假,真],dtype=bool)>>>r = 数组(范围(len(b)))>>>r(b)[2, 5]接下来应该可以工作(但我手头没有 Python 解释器来测试它):
class my_array(numpy.array):def find(self, b):r = 数组(范围(len(b)))返回 r(b)>>>a = my_array([1, 2, 3, 1, 2, 3])>>>a.find(a>2)[2, 5]
在 Python 中,您根本不会为此使用索引,而只是处理值——
[value for value in a if value >2]
.通常处理索引意味着你没有以最好的方式做某事.如果您确实需要一个类似于 Matlab 的 API,您可以使用 numpy,一个Python 中的多维数组和数值数学包,深受 Matlab 的启发.您将使用 numpy 数组而不是列表.
<预><代码>>>>导入 numpy>>>a = numpy.array([1, 2, 3, 1, 2, 3])>>>一种数组([1, 2, 3, 1, 2, 3])>>>numpy.where(a > 2)(数组([2, 5]),)>>>>2数组([假,假,真,假,假,真],dtype=bool)>>>[numpy.where(a > 2)]数组([3, 3])>>>[a>2]数组([3, 3])
The following Python code appears to be very long winded when coming from a Matlab background
>>> a = [1, 2, 3, 1, 2, 3]
>>> [index for index,value in enumerate(a) if value > 2]
[2, 5]
When in Matlab I can write:
>> a = [1, 2, 3, 1, 2, 3];
>> find(a>2)
ans =
3 6
Is there a short hand method of writing this in Python, or do I just stick with the long version?
Thank you for all the suggestions and explanation of the rationale for Python's syntax.
After finding the following on the numpy website, I think I have found a solution I like:
http://docs.scipy.org/doc/numpy/user/basics.indexing.html#boolean-or-mask-index-arrays
Applying the information from that website to my problem above, would give the following:
>>> from numpy import array
>>> a = array([1, 2, 3, 1, 2, 3])
>>> b = a>2
array([False, False, True, False, False, True], dtype=bool)
>>> r = array(range(len(b)))
>>> r(b)
[2, 5]
The following should then work (but I haven't got a Python interpreter on hand to test it):
class my_array(numpy.array):
def find(self, b):
r = array(range(len(b)))
return r(b)
>>> a = my_array([1, 2, 3, 1, 2, 3])
>>> a.find(a>2)
[2, 5]
In Python, you wouldn't use indexes for this at all, but just deal with the values—
[value for value in a if value > 2]
. Usually dealing with indexes means you're not doing something the best way.If you do need an API similar to Matlab's, you would use numpy, a package for multidimensional arrays and numerical math in Python which is heavily inspired by Matlab. You would be using a numpy array instead of a list.
>>> import numpy >>> a = numpy.array([1, 2, 3, 1, 2, 3]) >>> a array([1, 2, 3, 1, 2, 3]) >>> numpy.where(a > 2) (array([2, 5]),) >>> a > 2 array([False, False, True, False, False, True], dtype=bool) >>> a[numpy.where(a > 2)] array([3, 3]) >>> a[a > 2] array([3, 3])
这篇关于使用python列表理解根据条件查找元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!