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

在使用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 : dictionary
10        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     Examples
17     --------
18     >>> G=nx.path_graph(3)  # nodes 0-1-2
19     >>> 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..25
25     >>> mapping=dict(zip(G.nodes(),"abcdefghijklmnopqrstuvwxyz"))
26     >>> H=nx.relabel_nodes(G,mapping) # nodes a..z
27     >>> mapping=dict(zip(G.nodes(),range(1,27)))
28     >>> G1=nx.relabel_nodes(G,mapping) # nodes 1..26
29
30     Partial in-place mapping:
31
32     >>> G=nx.path_graph(3)  # nodes 0-1-2
33     >>> 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**2
44     >>> H=nx.relabel_nodes(G,mapping)
45     >>> print(H.nodes())
46     [0, 1, 4]
47
48     Notes
49     -----
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 Also
57     --------
58     convert_node_labels_to_integers
59     """
60     # you can pass a function f(old_label)->new_label
61     # but we‘ll just make a dictionary here regardless
62     if not hasattr(mapping,"__getitem__"):
63         m = dict((n, mapping(n)) for n in G)
64     else:
65         m = mapping
66     if copy:
67         return _relabel_copy(G, m)
68     else:
69         return _relabel_inplace(G, m)

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

时间: 2024-08-09 19:53:31

AttributeError: 'str' object has no attribute 'copy'的相关文章

Django项目与mysql交互进行数据迁移时报错:AttributeError: 'str' object has no attribute 'decode'

问题描述 Django项目启动,当我们执行命令 python manage.py makemigrations 出现如下错误: File "/usr/local/lib/python3.6/dist-packages/django/db/backends/mysql/operations.py", line 147, in last_executed_query query = query.decode(errors='replace') AttributeError: 'str' o

解决:pipenv shell报错:AttributeError: 'module' object has no attribute 'run'

利用pipenv shell切换到虚拟环境时,显示报错:AttributeError: 'module' object has no attribute 'run' 可以看到是d:\program\python34\lib\site-packages\pipenv\shells.py文件的第62行报错了,提示模块没有run的属性,于是就跑到该文件的第62行去看 选中run,CTRL+B发现能看到源码,源码如下: if sys.version_info >= (3, 6): # Nearly sa

AttributeError: 'NoneType' object has no attribute 'find'

遇到这个问题是因为读取excel表格的数据时默认是str类型,但是excel有空行时,类型读取为NoneType,如果把类型转换为str或者把excel里的空行删掉就不会有这个问题 AttributeError: 'NoneType' object has no attribute 'find' 原文地址:https://www.cnblogs.com/wangguniang/p/12101189.html

AttributeError: 'module' object has no attribute 'dumps'

报错: [[email protected] ~]# ./json.py DATA: [{'a': 'A', 'c': 3.0, 'b': (2, 4)}] Traceback (most recent call last): File "./json.py", line 4, in <module> import json File "/root/json.py", line 8, in <module> data_string = jso

Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attribute 'xxx'".这其实是.pyc文件存在问题. 问题定位: 查看import库的源文件,发现源文件存在且没有错误,同时存在源文件的.pyc文件 问题解决方法: 1. 命名py脚本时,不要与python预留字,模块名等相同 2. 删除该库的.pyc文件(因为py脚本每次运行时均会生成.pyc文件

AttributeError: &#39;dict_values&#39; object has no attribute &#39;translate&#39;

/***************************************************************************************** * AttributeError: 'dict_values' object has no attribute 'translate' * 说明: * 由于目前使用的是Python3,在解读MySQL的ORM库的时候,结果直接遇到这个错误. * * 2016-10-13 深圳 南山平山村 曾剑锋 **********

[Python]attributeError:&#39;module&#39; object has no attribute &#39;dump&#39;

[问题] [代码] 文件名:pickle.py # coding=utf-8 #持久存储 import pickle #b 以二进制的模式打开文件 with open('mydata.pickle','wb') as mysavedata: #用dump保存数据 pickle.dump([1,2,'three'],mysavedata) #b 以二进制的模式打开文件 with open('mydata.pickle','rb') as myreaddata: #使用load恢复数据 list =

python3 AttributeError: &#39;NoneType&#39; object has no attribute &#39;split&#39;

1 from wsgiref.simple_server import make_server 2 3 def RunServer(environ, start_response): 4 start_response('200 ok',[('Content-Type','text/html')]) 5 return '<h1>Hello world</h1>' 6 7 if __name__ == '__main__': 8 httpd = make_server('127.0.0

Faq_flask : AttributeError: ‘module’ object has no attribute ‘autoescape’

原文地址: http://www.suzf.net/thread-0613-153.html 转载须注明原始出处 前些天从 "spider_net" 上找了一篇文章 , 大致就是利用 highcharts + flask + mysql 构建的一个简单的监控系统 ,经过几番挣扎终于还是给捣持出来了 , 现在总结一下 , 分享给大家 .   o_O 部分报错信息: * Detected change in 'flask_web.py', reloading* Restarting wit