【问题标题】:Apply function into arr and print out将函数应用到 arr 并打印出来
【发布时间】:2021-01-06 17:16:57
【问题描述】:
def kiem_tra_so_hoan_chinh(so):
    if so <= 1:
        return False
    else:
        tong = 0
        for i in range(1, so):
            if so % i == 0:
                tong += i
        if tong == so:
            return True
        else:
            return False

         
        np.random.seed(5)
    arr1 = np.random.randint(low = 1, high = 101, size = 16) 
    arr1=arr1.reshape(4,4)
    arr1

如果 arr1 包含完美数字,我如何将函数用于数组并打印出 arr1 行 如果我使用kiem_tra_so_hoan_chinh(arr1),它会显示错误:

"ValueError: 多元素数组的真值不明确。使用a.any()或a.all()"

【问题讨论】:

    标签: python arrays function numpy


    【解决方案1】:

    方法一

    np.vectorizeany 与列表理解一起使用。注意:np.vectorize 只是一个花哨的 numpy for-loop

    arr2 = arr1[[np.vectorize(kiem_tra_so_hoan_chinh)(i).any() for i in arr1],:]
    
    Out[533]:
    array([[74,  9, 63, 28],
           [16, 54, 81, 28]])
    

    方法二:

    np.vectorizenp.apply_along_axis

    arr2 = arr1[np.apply_along_axis(np.vectorize(kiem_tra_so_hoan_chinh), 1, arr1)
                  .any(1),:]
    
    Out[540]:
    array([[74,  9, 63, 28],
           [16, 54, 81, 28]])
    

    方法三

    np.frompyfunc 带有列表理解:

    arr2 = arr1[[np.frompyfunc(kiem_tra_so_hoan_chinh,1,1)(i).any() for i in arr1],:]
    
    Out[545]:
    array([[74,  9, 63, 28],
           [16, 54, 81, 28]])
    

    【讨论】:

    • @DinhTruongAnhPhuong:不客气。快乐编码:)
    【解决方案2】:

    使用列表推导的类似方法应该可以解决问题。

    def kiem_tra_so_hoan_chinh(so):
        if so <= 1:
            return False
        else:
            tong = 0
            for i in range(1, so):
                if so % i == 0:
                    tong += i
            if tong == so:
                return True
            else:
                return False
        
    np.random.seed(5)
    arr1 = np.random.randint(low = 1, high = 101, size = 16) 
    # use iterator to apply function to each element
    is_perfect = np.array([kiem_tra_so_hoan_chinh(i) for i in arr1])
    is_perfect_reshaped=is_perfect.reshape(4,4)
    # each element is now a row of 4, so apply sum(row)>0 to find
    # at least one perfect number.
    contains_perfect = np.array([sum(row)>0 for row in is_perfect_reshaped])
    print(arr1.reshape(4,4))
    print(is_perfect_reshaped)
    print(contains_perfect)
    
    [[100  79  62  17]
     [ 74   9  63  28]
     [ 31  81   8  77]
     [ 16  54  81  28]]
    [[False False False False]
     [False False False  True]
     [False False False False]
     [False False False  True]]
    [False  True False  True]
    

    【讨论】:

    • 你能给我一个支持吗,我需要打印出这样的结果 [[ 74 9 63 28][ 16 54 81 28]].. 我试试这个代码 result = np.where( contains_perfect == True) result_arr=np.array([print(arr1[:,i]) for i in result]) Thr result_arr 是一维数组,而不是我想要的二维数组
    • 几点: - 在列表理解中编写 print 很奇怪,创建您的对象然后打印它。 - 您可以使用数组上的 .reshape 方法将 1D 数组重塑为 2D,就像您在上面所做的那样让我知道您是否还有其他挑战:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    相关资源
    最近更新 更多