博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AttributeError: 'str' object has no attribute 'copy'
阅读量:4615 次
发布时间:2019-06-09

本文共 2296 字,大约阅读时间需要 7 分钟。

在使用Python Networkx 中的relabel_nodes函数时,出现:

AttributeError: 'str' object has no attribute 'copy'

找了半天也没发现错误出现在哪里,通过比较发现:

如果在定义图的类型时候使用G=nx.MultiDiGraph()就会出现这个错误,改为G=nx.Graph()错误消失。

目测是relabel_nodes函数不支持MultiDiGraph。

附:

1 def relabel_nodes(G, mapping, copy=True): 2     """Relabel the nodes of the graph G. 3  4     Parameters 5     ---------- 6     G : graph 7        A NetworkX graph 8  9     mapping : dictionary10        A dictionary with the old labels as keys and new labels as values.11        A partial mapping is allowed.12 13     copy : bool (optional, default=True)14        If True return a copy, or if False relabel the nodes in place.15 16     Examples17     --------18     >>> G=nx.path_graph(3)  # nodes 0-1-219     >>> mapping={0:'a',1:'b',2:'c'}20     >>> H=nx.relabel_nodes(G,mapping)21     >>> print(sorted(H.nodes()))22     ['a', 'b', 'c']23 24     >>> G=nx.path_graph(26) # nodes 0..2525     >>> mapping=dict(zip(G.nodes(),"abcdefghijklmnopqrstuvwxyz"))26     >>> H=nx.relabel_nodes(G,mapping) # nodes a..z27     >>> mapping=dict(zip(G.nodes(),range(1,27)))28     >>> G1=nx.relabel_nodes(G,mapping) # nodes 1..2629 30     Partial in-place mapping:31 32     >>> G=nx.path_graph(3)  # nodes 0-1-233     >>> mapping={0:'a',1:'b'} # 0->'a' and 1->'b'34     >>> G=nx.relabel_nodes(G,mapping, copy=False)35 36     print(G.nodes())37     [2, 'b', 'a']38 39     Mapping as function:40 41     >>> G=nx.path_graph(3)42     >>> def mapping(x):43     ...    return x**244     >>> H=nx.relabel_nodes(G,mapping)45     >>> print(H.nodes())46     [0, 1, 4]47 48     Notes49     -----50     Only the nodes specified in the mapping will be relabeled.51 52     The keyword setting copy=False modifies the graph in place.53     This is not always possible if the mapping is circular.54     In that case use copy=True.55 56     See Also57     --------58     convert_node_labels_to_integers59     """60     # you can pass a function f(old_label)->new_label61     # but we'll just make a dictionary here regardless62     if not hasattr(mapping,"__getitem__"):63         m = dict((n, mapping(n)) for n in G)64     else:65         m = mapping66     if copy:67         return _relabel_copy(G, m)68     else:69         return _relabel_inplace(G, m)

 

转载于:https://www.cnblogs.com/purple-blog/p/4593786.html

你可能感兴趣的文章
[luogu4310] 绝世好题 (递推)
查看>>
[luogu3203 HNOI2010] 弹飞绵羊 (分块)
查看>>
mui搜索框 搜索点击事件
查看>>
2016012003+陈琦+散列函数的应用及其安全性
查看>>
Android 状态栏通知Notification、NotificationManager详解
查看>>
UIApplicationDelegate协议
查看>>
Jmeter测试dubbo接口填坑
查看>>
[zz]GDB调试精粹及使用实例
查看>>
数据库的创建和删除
查看>>
最简单的三层实例【插入据
查看>>
设计模式学习笔记——Prototype原型模式
查看>>
pom.xml里有红叉报错的解决办法
查看>>
Perl last和next的用法区别
查看>>
Selenium 管理 Cookies
查看>>
exceptionfunction[LeetCode]Permutations
查看>>
bzoj 4595 激光发生器
查看>>
multi cookie & read bug
查看>>
js时间转换
查看>>
(转载) Android Studio你不知道的调试技巧
查看>>
队列实现霍夫曼树
查看>>