一维数组到单整数并再次返回

时间:2018-08-18 10:10:51

标签: python arrays numpy reinforcement-learning

首先

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的解决方案。

2 个答案:

答案 0 :(得分:0)

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)

答案 1 :(得分:0)

我认为最好用暴力破解,但是长度不能超过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]
相关问题