【问题标题】:numpy multiplication table ouptutnumpy乘法表输出
【发布时间】:2018-04-20 17:26:58
【问题描述】:

我是 numpy 的新手,并试图找到一种使用 numpy 编写 mult 表的有效方法。

def mult_table():
    result = []
    for i in a:
        for j in a:
            result.append(i*j)
    return result

在 numpy 中,我看到一个点矩阵和一个 matmul,但不确定如何复制上述逻辑。

【问题讨论】:

标签: python arrays numpy multiplication


【解决方案1】:

一种方法是使用numpy.arange。您可以轻松地将其包装在一个函数中。

import numpy as np

def mult_table(n):
    rng = np.arange(1, n+1)
    return rng * rng[:, None]

print(mult_table(5))

# [[ 1  2  3  4  5]
#  [ 2  4  6  8 10]
#  [ 3  6  9 12 15]
#  [ 4  8 12 16 20]
#  [ 5 10 15 20 25]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 2022-12-13
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多