python3:没有足够的值可解压缩(预期2,得到0)

时间:2018-11-16 15:19:43

标签: python python-3.x

这是我的代码:

node2vec = {}
f = open('embed_hep.txt', 'rb')
for i, j in enumerate(f):  # i:index j:item
    if j != '\n':
        node2vec[i] = map(float, j.strip().decode("utf-8").split(' '))
f1 = open('test_graph.txt', 'rb')
edges = [map(int, i.strip().decode("utf-8").split('\t')) for i in f1]
nodes = list(set([i for j in edges for i in j]))
a = 0
b = 0
for i, j in edges:
    if i in node2vec.keys() and j in node2vec.keys():
        dot1 = np.dot(node2vec[i], node2vec[j])
        random_node = random.sample(nodes, 1)[0]
        while random_node == j or random_node not in node2vec.keys():
            random_node = random.sample(nodes, 1)[0]
        dot2 = np.dot(node2vec[i], node2vec[random_node])
        if dot1 > dot2:
            a += 1
        elif dot1 == dot2:
            a += 0.5
        b += 1

print(float(a) / b)

这是错误:

中的第14行
for i, j in edges:

ValueError:没有足够的值可解包(预期2,得到0)

embed_hep.txt:

1 3 6 8
3 5 7 0
3 6 8 9

text_graph.txt:

1698    2012
779     778
804     815

2 个答案:

答案 0 :(得分:1)

首先,您需要按照上面的建议转换地图。接下来,该错误变得微不足道。您的列表边缘是一个列表列表。因此,您要求它将其解压缩为没有的值。

node2vec = {}
with open('embed_hep.txt') as f:
    for idx, line in enumerate(f.readlines()):
        nodes  = [int(i) for i in line.strip().split()]
        node2vec[idx] = nodes

with open('test_graph.txt') as f:
    edges = [[int(j) for j in i.strip().split()] for i in f]
nodes = list(set([i for j in edges for i in j]))

for i, j in edges:
    print(i, j)

答案 1 :(得分:0)

map返回一个生成器而不是一个列表。

您的行

edges = [map(int, i.strip().decode("utf-8").split('\t')) for i in f1]

应该是

edges = list(map(int, i.strip().decode("utf-8").split('\t')) for i in f1)