【问题标题】:How to convert a nesting 'for' loops into one line of code in python如何将嵌套的'for'循环转换为python中的一行代码
【发布时间】:2020-04-02 06:19:26
【问题描述】:

我有这个代码示例,我的任务是将虚线内的行减少为一行代码,这意味着将所有 for 循环转换为一行代码。 我自己想出了一种解决方案:

myMat.ravel()[::size + 1] = myMat.ravel()[::size - 1] = 1

我认为除了我自己的解决方案之外,'np.logical_or' 函数可以通过数组切片的组合来帮助。
这是下面的功能。 谢谢。

def myXMat(size):
    if size != np.uint64(size):
        return None
    elif size == 1:
        return 1.0

    myMat = np.zeros((size, size), dtype=np.float64)
    # -------------------------------------------------
    for m in range(size):
        for n in range(size):
            if m == n or m == size - n - 1:
                myMat[m, n] = 1

    # -------------------------------------------------

    return myMat

【问题讨论】:

    标签: python numpy for-loop image-processing numpy-slicing


    【解决方案1】:

    让我们来看看大局;你在创造什么模式?

    In [343]: arr = np.zeros((10,10),int)                                                          
    In [344]: for m in range(10): 
         ...:     for n in range(10): 
         ...:         if m==n or m==10-n-1: 
         ...:             arr[m,n] = 1 
         ...:                                                                                      
    In [345]: arr                                                                                  
    Out[345]: 
    array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
           [0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
           [0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
           [0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
           [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
           [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
           [0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
           [0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
           [0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
           [1, 0, 0, 0, 0, 0, 0, 0, 0, 1]])
    

    所以这是对角线和它的对角线?

    In [346]: np.diag(np.ones(10,int))                                                             
    Out[346]: 
    array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])
    

    然后翻转它:

    In [347]: np.diag(np.ones(10,int))[::-1]                                                       
    Out[347]: 
    array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
           [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
           [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
           [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
           [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
           [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
           [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
           [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
           [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
           [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
    

    并将它们相加:

    In [348]: _346+_347                                                                            
    Out[348]: 
    array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
           [0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
           [0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
           [0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
           [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
           [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
           [0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
           [0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
           [0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
           [1, 0, 0, 0, 0, 0, 0, 0, 0, 1]])
    

    或带索引:

    In [352]: arr = np.zeros((10,10),int)                                                          
    In [353]: arr[np.arange(10),np.arange(10)] = 1                                                 
    In [354]: arr[np.arange(10)[::-1],np.arange(10)] = 1  
    

    【讨论】:

    • 使用 bit_wise 或 | 代替 + 和 np.diag 来处理奇数维度。
    猜你喜欢
    • 2019-11-10
    • 1970-01-01
    • 2022-01-24
    • 2020-11-26
    • 1970-01-01
    • 2021-12-30
    • 2016-09-17
    • 2019-04-14
    • 2018-11-30
    相关资源
    最近更新 更多