[英]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.