numpy乘法表输出 [英] numpy multiplication table ouptut

查看:72
本文介绍了numpy乘法表输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im a newbie to numpy and trying to find a efficient way to write mult table using numpy.

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

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

In numpy I see a dot matrix and a matmul but not sure how to replicate the above logic.

推荐答案

一种方法是使用

One way is to use numpy.arange. You can easily wrap this in a function.

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]]

这篇关于numpy乘法表输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆