简体   繁体   English

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

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

Input: A dictionary of lists. 输入:列表字典。

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

Output: A 2d array 输出:一个二维数组

Code: 码:

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

Error: 错误:

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

How do I push a list into a 2d array such that P[node][i] = ['3', '2', etc]? 如何将列表推入2d数组,使得P [node] [i] = ['3','2'等]?

On the line with P[int(node + 1)][0] = [str(int(node)+ 1)] I think you meant P[int(node + 1)][0] = p[str(int(node)+ 1)] . 在与P[int(node + 1)][0] = [str(int(node)+ 1)]的线上,我认为您的意思是P[int(node + 1)][0] = p[str(int(node)+ 1)] otherwise you assign a list to P[][] and pass it to int(...). 否则,将列表分配给P [] []并将其传递给int(...)。

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

相关问题 我可以使用列表理解在 python 的二维数组中获取输入吗?? 以及如何为 nD 数组实现它? - Can I Use the List Comprehensions for taking input in the 2D Array in the python ??. And how it can be implemented for the nD array? Python无法将值输入2D数组 - Python cannot input values into 2d array 如何使用用户输入修改列表(二维数组)? (Python) - How to modify a list (2D Array) using user input? (Python) 如何在二维列表 python 中输入不同数据类型的值? - how can i input values of different data types in 2d list python? 如何在 python 中以类似盒子的方式将几个 2D arrays 合并到一个 2D 数组(列表)中? - How do I merge several 2D arrays into a single 2D array (list) in a box-like manner in python? 如何比较存储在二维数组中的值? - How do I compare values that I stored in a 2d array? 如何在 python 中使用 for 循环将值添加到二维数组中 - How do i add the values in a 2d array using a for loop in python 如何从二维数组中选择值并添加到另一个二维数组 python - How can i select values from 2d array and add to another 2d array python 如何使用列表理解将 Python 中的二维数组合并为一个字符串? - How do I merge a 2D array in Python into one string with List Comprehension? 如何在 python 中将平面列表转换为二维数组? - How can I turn a flat list into a 2D array in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM