简体   繁体   English

您可以在括号中的if-else语句用于Python中的数组吗?

[英]Can you use if-else statement inside braces for an array in Python?

I'm wondering if there is a nice way to use an if-else statement inside braces of an array in Python to assign values. 我想知道是否有很好的方法在Python数组的括号内使用if-else语句来分配值。 What I would like is something like: 我想要的是这样的:

A = #some 2D array of length m by n, already initialized
A = np.float64(A)
val = someValue #any number, pick a number

A = [[val for j in range(n) if A[i][j] < val, else A[i][j]=A[i][j]] for i in range(m)]

Is there a nice way to do this? 有没有很好的方法可以做到这一点? Alternatively, if numpy has a faster way to compute this that would be equally as good, if not better. 另外,如果numpy有更快的方法来计算,那同样好,甚至更好。

The longer way to do what I am trying to achieve would be something like 做我想达到的目标的更长的方法是

for i in range(m):
    for j in range(n):
        if A[i][j] < val:
            A[i][j] = val

The desired output is to set any values below a threshold to that threshold. 期望的输出是将低于阈值的任何值设置为该阈值。 I can do simpler if-statements with a 1D array such as 我可以使用一维数组执行更简单的if语句,例如

myArray = [otherArray[i] for i in range(theRange) if otherArray[i]>=value and otherArray[i]<=anotherValue]

This 1D example is not what I want. 这个一维示例不是我想要的。 It's just an example of the type of coding block I'm looking for. 这只是我要寻找的编码块类型的一个示例。 It seems to be quicker at processing against the traditional if-else statements. 与传统的if-else语句相比,它似乎处理起来更快。

With numpy arrays we try avoid iteration (list comprehension). 对于numpy数组,我们尝试避免迭代(列表理解)。 Sometimes it is needed, but in this case it is not: 有时是必需的,但在这种情况下,它不是:

In [403]: A=np.arange(16).reshape(4,4)    
In [404]: A1=A.astype(np.float64)    # better syntax for converting to float

In [405]: A1
Out[405]: 
array([[  0.,   1.,   2.,   3.],
       [  4.,   5.,   6.,   7.],
       [  8.,   9.,  10.,  11.],
       [ 12.,  13.,  14.,  15.]])

A boolean array that shows where the test is True/False: 一个布尔数组,显示测试在哪里正确/错误:

In [406]: A1<5 
Out[406]: 
array([[ True,  True,  True,  True],
       [ True, False, False, False],
       [False, False, False, False],
       [False, False, False, False]], dtype=bool)

We can index with such a mask: 我们可以使用这样的掩码进行索引:

In [407]: A1[A1<5]=5

In [408]: A1
Out[408]: 
array([[  5.,   5.,   5.,   5.],
       [  5.,   5.,   6.,   7.],
       [  8.,   9.,  10.,  11.],
       [ 12.,  13.,  14.,  15.]])

np.where (and np.nonzero ) return indices where the condition is True; np.where (和np.nonzero )返回条件为True的索引; where has a version that operates like the ternary operator (on each element): where具有一个像三元运算符一样运行的版本(在每个元素上):

In [410]: np.where(A<5,5,A)
Out[410]: 
array([[ 5,  5,  5,  5],
       [ 5,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

We can also clip with np.maximum : 我们还可以使用np.maximum clip

In [411]: np.maximum(A,5)
Out[411]: 
array([[ 5,  5,  5,  5],
       [ 5,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

In [417]: A.clip(5,None)
Out[417]: 
array([[ 5,  5,  5,  5],
       [ 5,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

Python's one-line ternary operator syntax looks like this Python的单行三元运算符语法如下所示

variable = a if CONDITION else b

You can place this inside a list comprehension as well. 您也可以将其放在列表理解中。 It's not clear what val is in your example, but I'm assuming it's a value you specified beforehand. 目前尚不清楚您的示例中的val是什么,但我假设它是您事先指定的值。

val = 2
A = [[val if A[i][j] < val else A[i][j] for j in range(n)] for i in range(m)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM