繁体   English   中英

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

[英]ValueError:not enough values to unpack (expected 2, got 0)

在python3.6上,(我的数据集graph.txt如下:在此处输入图像描述

每一行都被 \\t 分割

我的代码:

   text_file, graph_file = self.load(text_path, graph_path)

   self.edges = self.load_edges(graph_file)

def load(self, text_path, graph_path):
    text_file = open(text_path, 'rb').readlines()
    graph_file = open(graph_path, 'rb').readlines()
    return text_file, graph_file

def load_edges(self, graph_file):
    edges = []
    for i in graph_file:
        edges.append(map(int, i.strip().decode("utf-8").split('\t')))
    return edges

但是,当我运行以下功能时:

def negative_sample(self, edges):
    node1, node2 = zip(*edges)
    sample_edges = []
    func = lambda:self.negative_table[random.randint(0,config.neg_table_size - 1)]
    for i in range(len(edges)):
        neg_node = func()
        while node1[i] == neg_node or node2[i] == neg_node:neg_node = func()
        sample_edges.append([node1[i], node2[i], neg_node])
    return sample_edges

错误:

值错误:在函数中:negative_sample node1, node2 = zip(*edges)

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

我已经解决了这个问题。 在 python2 中,map 返回一个列表,但在 python3.x 中,map ruturn 是一个可迭代对象,因此代码应更改为:

   edges.append(list(map(int, i.strip().decode("utf-8").split('\t'))))

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM