【问题标题】:reading a csv file into a Networkx graph in python 3.5在 python 3.5 中将 csv 文件读入 Networkx 图
【发布时间】:2016-11-28 04:25:56
【问题描述】:

我正在关注本教程:graph tutorial 在我自己的数据文件中。但我被困在最后一点。代码似乎抛出:

(most recent call last)<ipython-input-193-7227d35394c0> in <module>()
      1 for node in links:  #loops through each link and changes each dictionary to a tuple so networkx can read in the information
      2     edges = node.items()
----> 3     G.add_edge(*edges[0])  #takes the tuple from the list and unpacks the tuples
      4 
      5 nx.draw(G)
TypeError: 'dict_items' object does not support indexing

有解决办法吗?我有点卡住了。

【问题讨论】:

    标签: python-3.x csv networkx


    【解决方案1】:

    这可能是 python-2 唯一兼容的代码。

    在 python 2 中,dict.items() 返回一个元组列表(键、值),而不是dict.iteritems()

    在 python 3 中,dict.iteritems() 已被删除,dict.items() 返回一个迭代器。 因此,您必须将其显式转换为 list 才能通过索引访问元素:

    edges = list(node.items())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      相关资源
      最近更新 更多