如何使某个整数的numpy数组下面的数字的所有数字组合 [英] How to make all number combinations of numbers below a certain numpy array of integers

查看:114
本文介绍了如何使某个整数的numpy数组下面的数字的所有数字组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特定的numpy整数数组,我想创建一个数组数组,其中包含下面的所有数字组合。例如,[2,3]将产生

I have a certain numpy array of integers and I want to make an array of arrays that contains all combinations of numbers below it. As an example, [2,3] would yield

[[1,1],[2,1],[1,2],[2,2],[1,3],[3,3]] 

因为这将是数字小于或等于[2,3]的数组的所有组合。

because that would be all the combinations of arrays with numbers less than or equal to [2,3].

推荐答案

使用 itertools.product

from itertools import product

a,b = 2,3

list(product(np.arange(a)+1,np.arange(b)+1))

输出:

[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3)]

这篇关于如何使某个整数的numpy数组下面的数字的所有数字组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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