将1D数组转换为单个整数,然后再返回

2024-04-23 17:42:08 发布

您现在位置:Python中文网/ 问答频道 /正文

首先

import numpy as np

a = np.random.rand(84)
b = [int(round(i)) for i in a]

c = makeint(b)

d = makelist(c)

如果我要做:

>>> b
[0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1]

>>> type(b)
<class 'list'>

>>> type(c)
<class 'int'>

>>> type(d)
<class 'list'>

>>> d == b
True

我找了又找,什么也没找到。只是澄清一下,我并不是想做[1,2,3,4]->;[1234]

更像:

[0,0,0,0]->;0

[1,0,0,0]->;1

[0,1,0,0]->;2

[0,0,1,0]->;3

。。。你知道吗

[1,0,1,1]->。。。你知道吗

但对于每一个可能的组合。你知道吗

我已经看过itertools.combinations,但在这种情况下没有任何用处。真正需要的是一个数据库,上面的所有组合都是行号,因此:

y = [[0,0,0,0], [1,0,0,0], [0,1,0,0], [0,0,1,0]。。。你知道吗

def makeint(l):
    global y
    return y.index(l)

def makelist(i):
    global y
    return y[i]

这是功能性的:

>>> c = makeint([0,0,1,0])

>>> c
3

>>> makelist(c)
[0,0,1,0]

但确实产生了y。对于一个可能有万亿次组合的数组,我们需要有效地生成y。这个数组只有10,如果有人能推荐一种尽可能高效地生成y的方法或者一个模块,但是我更喜欢numpy、scipy或pandas,我不想使用模块。我正在寻找一个可扩展的,简洁的和pythonic解决方案。你知道吗


Tags: 模块importgtnumpyreturndeftypenp
2条回答
def createlist(x):
    retList=[]
    a,b,c,d=0,0,0,0
    a=calModFunc(x,4,retList)
    b=calModFunc(a[0],3,a[1])
    c=calModFunc(b[0],2,b[1])
    if c[0]==1:
        c[1].append(1)
    else:
        c[1].append(0)
    return retList

def calModFunc(x,n,retList):
    if x<n:
        temp=x
        retList.append(0)
    elif x==n:
        temp=x%n
        retList.append(1)
    else:
        temp=x-n
        if(temp!=0):
            retList.append(1)
        else:
            retList.append(0)
    return temp,retList


def makelist(x):
    reverseList=createlist(x)[::-1]
    return reverseList

Please try above code. Implementation : makelist(num)

我认为最好是强行执行,但不能超过16个长度,否则,您将遇到如下内存限制: task manager 代码如下:

import numpy as np
import itertools as it

def getblock(targ_len):
    start = [[0,0],[0,0],[0,1],[0,1],[1,0],[1,0],[1,1],[1,1]]

    sp = (8,2,1)

    while sp[1]*sp[2] < targ_len:
        a = it.permutations(start,2)
        b = np.array(list(a))

        sp = b.shape

        c = np.reshape(b, (sp[0],sp[1]*sp[2]))

        start = c.copy()

    return start

现在我们可以生成y

>>> y = getblock(16)
>>> y
array([[0, 0, 0, ..., 0, 0, 1],
   [0, 0, 0, ..., 0, 1, 0],
   [0, 0, 0, ..., 0, 1, 0],
   ...,
   [1, 1, 1, ..., 1, 0, 1],
   [1, 1, 1, ..., 1, 0, 1],
   [1, 1, 1, ..., 1, 1, 0]])

>>> a = getblock(4)
>>> b = [list(i) for i in a]
>>> b
[[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 1], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 1], [0, 0, 1, 1], [0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 1], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 1], [0, 1, 1, 1], [0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 1], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 1], [0, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 1], [1, 0, 0, 1], [1, 0, 1, 0], [1, 0, 1, 1], [1, 0, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 1], [1, 0, 0, 1], [1, 0, 1, 0], [1, 0, 1, 1], [1, 0, 1, 1], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 1], [1, 1, 0, 1], [1, 1, 1, 0], [1, 1, 1, 0], [1, 1, 1, 1], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 1], [1, 1, 0, 1], [1, 1, 1, 0], [1, 1, 1, 0], [1, 1, 1, 1]]

小心,这会造成重复。你知道吗

uniq = []
for x in b:
    if x not in uniq:
        uniq.append(x)

>>>uniq
[[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 1, 1], [0, 1, 0, 0], [0, 1, 0, 1], [0, 1, 1, 0], [0, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 1], [1, 0, 1, 0], [1, 0, 1, 1], [1, 1, 0, 0], [1, 1, 0, 1], [1, 1, 1, 0], [1, 1, 1, 1]

相关问题 更多 >