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

时间:2018-10-22 03:46:38

标签: python python-3.x

在python3.6上, (我的数据集graph.txt如下所示: enter image description here

,每行除以\ 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)

1 个答案:

答案 0 :(得分:1)

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

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