Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有一个简单的特性可以基于Python中的值条件构建子列表?_Python_Matlab_List - Fatal编程技术网

是否有一个简单的特性可以基于Python中的值条件构建子列表?

是否有一个简单的特性可以基于Python中的值条件构建子列表?,python,matlab,list,Python,Matlab,List,在Matlab中,当您希望在给定某些值条件的向量中获得值的子集时,请执行以下操作: negative_values = vec(vec<0) positive_values = vec(vec>0) 负值=vec(vec0) 我目前正在使用一个自制的函数在Python中实现这一点,但这有点繁重。是否有一种更优雅的方式继续进行或一个标准的功能,我不知道?我希望能够简明扼要地做类似的事情 negative_values = val.index(val<0) positive_v

在Matlab中,当您希望在给定某些值条件的向量中获得值的子集时,请执行以下操作:

negative_values = vec(vec<0)
positive_values = vec(vec>0)
负值=vec(vec0)
我目前正在使用一个自制的函数在Python中实现这一点,但这有点繁重。是否有一种更优雅的方式继续进行或一个标准的功能,我不知道?我希望能够简明扼要地做类似的事情

negative_values = val.index(val<0)
positive_values = val.index(val>0)
负值=val.index(val0)

但是显然,这不适用于
list.index()
,因为它不应该将表达式作为参数。

您可以将该语法用于
numpy

import numpy

a = numpy.arange(10)
--> a = array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

a[a > 5]
--> array([6, 7, 8, 9])

您可以将该语法用于
numpy

import numpy

a = numpy.arange(10)
--> a = array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

a[a > 5]
--> array([6, 7, 8, 9])

您可以像这样使用列表理解作为过滤器

numbers = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

negatives = [number for number in numbers if number < 0]
print negatives
# [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]

positives = [number for number in numbers if number >= 0]
print positives
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
negatives = filter(lambda number: number <  0, numbers)
positives = filter(lambda number: number >= 0, numbers)

您可以像这样使用列表理解作为过滤器

numbers = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

negatives = [number for number in numbers if number < 0]
print negatives
# [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]

positives = [number for number in numbers if number >= 0]
print positives
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
negatives = filter(lambda number: number <  0, numbers)
positives = filter(lambda number: number >= 0, numbers)

您需要使用
numpy
,它是作为matlab的替代品设计的:

In [1]: import numpy as np

In [2]: a = np.arange(-5, 5)

In [3]: a
Out[3]: array([-5, -4, -3, -2, -1,  0,  1,  2,  3,  4])

In [4]: a[a>0]
Out[4]: array([1, 2, 3, 4])

In [5]: np.where(a>0)  #used to find the indices where the condition matches
Out[5]: (array([6, 7, 8, 9]),)

In [6]: np.where(a%2==0)
Out[6]: (array([1, 3, 5, 7, 9]),)

您需要使用
numpy
,它是作为matlab的替代品设计的:

In [1]: import numpy as np

In [2]: a = np.arange(-5, 5)

In [3]: a
Out[3]: array([-5, -4, -3, -2, -1,  0,  1,  2,  3,  4])

In [4]: a[a>0]
Out[4]: array([1, 2, 3, 4])

In [5]: np.where(a>0)  #used to find the indices where the condition matches
Out[5]: (array([6, 7, 8, 9]),)

In [6]: np.where(a%2==0)
Out[6]: (array([1, 3, 5, 7, 9]),)

Lambda表达式<代码>正值=过滤器(λx:x>0,向量)。唯一的清理方法就是使用numpy或更改语言。这是我希望香草Python拥有的东西之一。Lambda表达式<代码>正值=过滤器(λx:x>0,向量)。唯一的清理方法就是使用numpy或更改语言。这是我希望vanilla Python拥有的东西之一。这个答案很好,因为它只使用标准函数。然而,我认为如果我没有任何空间或时间限制,那么使用其他答案中提到的
numpy
,会更方便。这个答案很好,因为它只使用标准函数。然而,我认为如果我没有任何空间或时间限制,那么使用其他答案中提到的
numpy
,会更方便。事实正好如此。