Python 如何使用numpy生成楼梯编号序列

Python 如何使用numpy生成楼梯编号序列,python,numpy,Python,Numpy,我试图使用python生成一个数字序列列表,如下所示 [0,0,0,0] [0,0,0,1] [0,0,0,2] [0,0,1,1] [0,0,1,2] [0,0,2,2] [0,1,1,1] [0,1,1,2] [0,1,2,2] [0,2,2,2] [1,1,1,1] [1,1,1,2] ... [2,2,2,2] 现在,我可以通过递归调用使用纯python来实现这一点,但是单次运行几小时需要花费大量时间。我想知道是否有可能用numpy来实现这一点并节省大量的时间,如果有,怎么做?你的意思

我试图使用python生成一个数字序列列表,如下所示

[0,0,0,0] [0,0,0,1] [0,0,0,2] [0,0,1,1] [0,0,1,2] [0,0,2,2] [0,1,1,1]
[0,1,1,2] [0,1,2,2] [0,2,2,2] [1,1,1,1] [1,1,1,2] ... [2,2,2,2]

现在,我可以通过递归调用使用纯python来实现这一点,但是单次运行几小时需要花费大量时间。我想知道是否有可能用numpy来实现这一点并节省大量的时间,如果有,怎么做?

你的意思是这一点还是你的意思是如何定义序列

from itertools import product

for item in product((0, 1, 2), repeat=4):
    print(item)
这张照片是:

(0, 0, 0, 0)
(0, 0, 0, 1)
(0, 0, 0, 2)
(0, 0, 1, 0)
(0, 0, 1, 1)
(0, 0, 1, 2)
...
(2, 2, 1, 2)
(2, 2, 2, 0)
(2, 2, 2, 1)
(2, 2, 2, 2)
不确定这是否是您想要的,但它包含在python中

这应该是快速和内存效率。列表是根据需要创建的

…仔细想想:这可能就是你的意思,对吧

for a, b, c, d in product((0, 1, 2), repeat=4):
    if not a <= b <= c <= d:
        continue
    print(a,b,c,d)
现在我明白了,你是多么希望这样更有效率


看起来就是这样。

你是说这个还是说序列是如何定义的

from itertools import product

for item in product((0, 1, 2), repeat=4):
    print(item)
这张照片是:

(0, 0, 0, 0)
(0, 0, 0, 1)
(0, 0, 0, 2)
(0, 0, 1, 0)
(0, 0, 1, 1)
(0, 0, 1, 2)
...
(2, 2, 1, 2)
(2, 2, 2, 0)
(2, 2, 2, 1)
(2, 2, 2, 2)
不确定这是否是您想要的,但它包含在python中

这应该是快速和内存效率。列表是根据需要创建的

…仔细想想:这可能就是你的意思,对吧

for a, b, c, d in product((0, 1, 2), repeat=4):
    if not a <= b <= c <= d:
        continue
    print(a,b,c,d)
现在我明白了,你是多么希望这样更有效率


看起来正是如此。

您正在寻找的是。从文档中:

组合_与_替换'ABCD',2 AA AB AC AD BB BC BD CC CD DD

因此:

>>> import itertools as it
>>> list(it.combinations_with_replacement((0, 1, 2), 4))
[(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 0, 2),
 (0, 0, 1, 1), (0, 0, 1, 2), (0, 0, 2, 2),
 (0, 1, 1, 1), (0, 1, 1, 2), (0, 1, 2, 2),
 (0, 2, 2, 2), (1, 1, 1, 1), (1, 1, 1, 2),
 (1, 1, 2, 2), (1, 2, 2, 2), (2, 2, 2, 2)]
这个方法最好的部分是,因为它返回一个生成器,所以您可以在不存储它的情况下对它进行迭代。这是一个巨大的优势,因为它将为您节省大量内存

其他实现和计时 这里还有一些实现,包括numpy实现。组合_与_替换try2功能似乎是最快的:

import itertools as it
import timeit

import numpy as np

def try1(n, m):
    return [t for t in it.product(range(n), repeat=m) if all(a <= b for a, b in zip(t[:-1], t[1:]))]

def try2(n, m):
    return list(it.combinations_with_replacement(range(n), m))

def try3(n, m):
    a = np.mgrid[(slice(0, n),) * m] # All points in a 3D grid within the given ranges
    a = np.rollaxis(a, 0, m + 1)     # Make the 0th axis into the last axis
    a = a.reshape((-1, m))           # Now you can safely reshape while preserving order
    return a[np.all(a[:, :-1] <= a[:, 1:], axis=1)]

>>> %timeit b = try1(3, 4)
10000 loops, best of 3: 78.1 µs per loop
>>> %timeit b = try2(3, 4)
The slowest run took 8.04 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.66 µs per loop
>>> %timeit b = try3(3, 4)
10000 loops, best of 3: 97.8 µs per loop
注:

我用的是蟒蛇3 我用于实现try1。 我用于实现try3。
你要找的是。从文档中:

组合_与_替换'ABCD',2 AA AB AC AD BB BC BD CC CD DD

因此:

>>> import itertools as it
>>> list(it.combinations_with_replacement((0, 1, 2), 4))
[(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 0, 2),
 (0, 0, 1, 1), (0, 0, 1, 2), (0, 0, 2, 2),
 (0, 1, 1, 1), (0, 1, 1, 2), (0, 1, 2, 2),
 (0, 2, 2, 2), (1, 1, 1, 1), (1, 1, 1, 2),
 (1, 1, 2, 2), (1, 2, 2, 2), (2, 2, 2, 2)]
这个方法最好的部分是,因为它返回一个生成器,所以您可以在不存储它的情况下对它进行迭代。这是一个巨大的优势,因为它将为您节省大量内存

其他实现和计时 这里还有一些实现,包括numpy实现。组合_与_替换try2功能似乎是最快的:

import itertools as it
import timeit

import numpy as np

def try1(n, m):
    return [t for t in it.product(range(n), repeat=m) if all(a <= b for a, b in zip(t[:-1], t[1:]))]

def try2(n, m):
    return list(it.combinations_with_replacement(range(n), m))

def try3(n, m):
    a = np.mgrid[(slice(0, n),) * m] # All points in a 3D grid within the given ranges
    a = np.rollaxis(a, 0, m + 1)     # Make the 0th axis into the last axis
    a = a.reshape((-1, m))           # Now you can safely reshape while preserving order
    return a[np.all(a[:, :-1] <= a[:, 1:], axis=1)]

>>> %timeit b = try1(3, 4)
10000 loops, best of 3: 78.1 µs per loop
>>> %timeit b = try2(3, 4)
The slowest run took 8.04 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.66 µs per loop
>>> %timeit b = try3(3, 4)
10000 loops, best of 3: 97.8 µs per loop
注:

我用的是蟒蛇3 我用于实现try1。 我用于实现try3。
花时间阅读这篇关于如何撰写好的SO问题的帖子:提示:包括您尝试过的代码。花时间阅读这篇关于如何撰写好的SO问题的帖子:提示:包括您尝试过的代码。