【问题标题】:TypeError: list indices must be integers or slicesTypeError:列表索引必须是整数或切片
【发布时间】:2019-10-29 06:24:05
【问题描述】:

我正在尝试可视化网络。信息是从 .csv 文件中读取的。所有节点都是列表中的对象。对象也包含一个列表。我一直遇到 TypeError 错误:TypeError: list indices must be integers or slices, not Router

我尝试在第二个 for 循环中进行转换,各种循环,但每当我尝试第二个循环时,它就会中断

将节点之间的边添加到网络

for a in node_list:
    for e in node_list[a].dst:
        ams_net.add_edge(node_list[e].name, node_list[e].dst[e])

我需要它遍历 node_list 中的所有节点,并且必须为每个目的地创建一个具有“边缘”的节点...

【问题讨论】:

    标签: python loops typeerror


    【解决方案1】:

    试试这个:

    for a in node_list:
        for e in a.dst:
            ecp_net.add_edge(a.name, e)
    

    因为ae 不是索引,而是列表中的元素

    【讨论】:

    • 感谢您的快速回复!现在我遇到了以下问题: e 现在被视为一个 str。 AttributeError:“str”对象没有属性“name”
    • 你能多分享一点你的代码吗?我不知道node_list 长什么样子,它包含什么元素。
    • 我想我明白了!使用了 a.name 和 a.dst。非常感谢
    • python for a in node_list: for e in a.dst: ecp_net.add_edge(a.name, e) 做到了,事后看来很有意义
    • 如果我帮助了您,并且您认为我的回答包含正确的解决方案,您也可以接受:stackoverflow.com/help/someone-answers
    猜你喜欢
    • 2016-09-16
    • 1970-01-01
    • 2019-07-04
    • 2015-12-09
    • 2021-11-28
    • 2020-09-23
    • 2020-09-04
    • 2019-10-01
    相关资源
    最近更新 更多