Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 切片时的if语句_Python_List_Numpy_Slice - Fatal编程技术网

Python 切片时的if语句

Python 切片时的if语句,python,list,numpy,slice,Python,List,Numpy,Slice,我有许多正在切片的列表,例如(使用示例数据): 然后我有一个循环,从大于或小于中点的第一个值的数字中查找索引位置。我有: for i in range(len(Values)): indexgtr = numbers[Values[i]>=midpoint[i]][-1] # The position of the first number larger than the midpoint indexlt = numbers[Values[i]<=midpoint[i

我有许多正在切片的列表,例如(使用示例数据):

然后我有一个循环,从大于或小于
中点的第一个值的
数字
中查找索引位置。我有:

for i in range(len(Values)):
    indexgtr = numbers[Values[i]>=midpoint[i]][-1] # The position of the first number larger than the midpoint
    indexlt = numbers[Values[i]<=midpoint[i]][0] # The position of the first number larger than the midpoint
范围内i的
(len(值)):
indexgtr=numbers[Values[i]>=中点[i][-1]#大于中点的第一个数字的位置

indexlt=numbers[Values[i]您可以沿第二个轴矢量化和使用所需的结果:

>>> midpoint = np.array([[0.2], [0.5], [0.6], [0.3]])
>>> values = np.array([[0.1, 0.3, 0.6, 0.8],
                       [0.2, 0.3, 0.5, 0.7],
                       [0.2, 0.5, 0.6, 0.9],
                       [0.3, 0.1, 0.8, 0.9]])

>>> (values > midpoint).argmax(axis=1) # indexgtr vectorized
array([1, 3, 3, 2]) # first >= occurrence for each row

>>> (values < midpoint).argmax(axis=1)
array([0, 0, 0, 1]) # first < occurrence for each row
中点=np.数组([[0.2]、[0.5]、[0.6]、[0.3]) >>>values=np.array([[0.1,0.3,0.6,0.8], [0.2, 0.3, 0.5, 0.7], [0.2, 0.5, 0.6, 0.9], [0.3, 0.1, 0.8, 0.9]]) >>>(值>中点)。argmax(轴=1)#索引GTR矢量化 数组([1,3,3,2])#第一次>=每行的出现次数 >>>(值<中点).argmax(轴=1) 数组([0,0,0,1])#每行第一次<出现次数


注意:我已将
=
替换为
,以更好地显示矢量化结果。请注意,对于
有多种方法可以表示测试:

for i in range(len(Values)):
    indexgtr = numbers[Values[i]>=midpoint[i]]
    if indexgtr.shape[0]==0:
        indexgtr = 0
    else:
        indexgtr = indexgtr[-1]
    indexlt = numbers[Values[i]<=midpoint[i]]
    if indexlt.shape[0]:       # alt expression
        indexlt = indexlt[0]
    else:
        indexlt = 0
    # indexlt = indexlt[0] if len(indexlt) else 0

我假设在进入下一个
I

之前,您将在这个循环中对
indexgtr
indexlt
执行一些操作。我得到的错误与您的错误不同,运行代码时得到的错误是我预期的。首先,表达式
值[I]>=中点[I]
列表
与标量值进行比较,在Python 2中只返回一个布尔值(Python 3给出无序类型错误)。因此,如果
为True,则访问
数字[1]
,如果
为False,则访问
数字[0]
,然后尝试进一步访问
数字[1][-1]
数字[0][1]
,这将给出一个错误,因为
数字[i]
是一个无法为每个
i
编制索引的标量。上面的数据只是一个示例,因为我的真实数据非常大,解释起来更复杂。我知道它返回布尔值,但它不仅仅是
True
False
,它更
[真,真,假,假,假]
取决于列表的长度。这将适当地分割
数字
,并向我提供索引位置
[0]
[-1]
按要求,除非我弄错了?很好,但我认为
np.argwhere
np.argmax
更合适,因为您处理的是一个boolarray@wim将返回所有元素(满足条件)的索引并且需要额外的后处理步骤来过滤每行的第一个元素。这基本上就是我最后所做的。我实际上将值附加到一个新列表中,所以像你的答案一样,我设置了一个
if
循环来检查数组的长度。如果它是空的,那么我会附加一个值0.0,否则我会运行我的代码。它也是空的很冗长,但不贵。
for i in range(len(Values)):
    indexgtr = numbers[Values[i]>=midpoint[i]]
    if indexgtr.shape[0]==0:
        indexgtr = 0
    else:
        indexgtr = indexgtr[-1]
    indexlt = numbers[Values[i]<=midpoint[i]]
    if indexlt.shape[0]:       # alt expression
        indexlt = indexlt[0]
    else:
        indexlt = 0
    # indexlt = indexlt[0] if len(indexlt) else 0
In [39]: x=np.arange(0)
In [40]: x[0] if len(x) else 0
Out[40]: 0