繁体   English   中英

如何在python的2d数组中输入值列表?

[英]How do I input a list of values into a 2d array in python?

输入:列表字典。

{'5': ['2', '4', '8'], '6': ['3', '1', '7'], '9': ['4', '8'], '3': ['2', '4'], '1': ['2'], '8': ['2'], '7': ['4'], '2':[], '4':[]}

输出:一个二维数组

码:

def preprocess(p=p):
    """
    This function creates a sparse table of 2^ith ancestor of the node
    """
    N = int(len(p))
    LEVEL = int(ceil(log(N)))
    P = [[-1 for i in range(LEVEL)] for node in range(N+1)]
    for node in range(0, N):
        #print(node)
        P[int(node + 1)][0] = p[str(int(node)+ 1)]
        if P[int(node + 1)][0] == []:
            P[int(node + 1)][0] = [str(int(node)+ 1)]
    for i in range(1, LEVEL):
        for node in range(N + 1):
            #print("node", node, "i", i)
            if P[node][i-1] != -1:
                #print("node not equal -1:", node, "i", i, i - 1)
                inside = P[node][i-1] # this wont work for dags
                print("inside", inside, type(inside))
                P[node][i] = P[int(inside)][i-1]
    return P

错误:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

如何将列表推入2d数组,使得P [node] [i] = ['3','2'等]?

在与P[int(node + 1)][0] = [str(int(node)+ 1)]的线上,我认为您的意思是P[int(node + 1)][0] = p[str(int(node)+ 1)] 否则,将列表分配给P [] []并将其传递给int(...)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM