输入:列表字典。
{'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'
如何将列表推入二维数组,使P [node] [i] = ['3','2'等]?
答案 0 :(得分:0)
与P[int(node + 1)][0] = [str(int(node)+ 1)]
一致,我认为你的意思是P[int(node + 1)][0] = p[str(int(node)+ 1)]
。否则你将一个列表分配给P [] []并将其传递给int(...)。