python3:没有足够的值来解压缩(应为2,得到0)

2024-05-23 20:01:28 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码:

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行,英寸

^{pr2}$

ValueError:没有足够的值来解压缩(应为2,得到0)

嵌入_hep.txt文件公司名称:

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

文本_图形.txt公司名称:

1698    2012
779     778
804     815

Tags: intxtnodeforifrandomopenkeys
2条回答

首先,您需要按照上面的建议转换地图。接下来,错误变得非常小。列表边缘是一个列表列表。因此,您要求它解压到它没有的值。在

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)

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

你的台词

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

应该是

^{pr2}$

相关问题 更多 >