【问题标题】:ValueError:not enough values to unpack (expected 2, got 0)ValueError:没有足够的值来解包(预期 2,得到 0)
【发布时间】:2018-10-22 14:29:45
【问题描述】:

在 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)

【问题讨论】:

  • node1, node2 = zip(*edges) 看起来不对。您应该查看 zip() 的作用。
  • 请以文本形式发布您的数据样本
  • 你到底是什么edges
  • 您的文件中可能有一个空行,需要将其过滤掉。
  • my data sample look like this: 440 50 440 103 440 105 440 116 440 135 440 235 440 257 440 353 440 380 454 255 454 404 511 440 511 471 511 478 519 105 519 353 519 440 519 478 519 511 584 478 584 511 584 539 693 116 693 475 693 584

标签: python python-3.x


【解决方案1】:

我已经解决了这个问题。在python2中,map返回一个列表,但是对于python3.x,map ruturn一个iterable,所以代码应该改变如下:

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

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 2022-01-21
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 2019-01-05
    • 2018-04-21
    相关资源
    最近更新 更多