1. 程式人生 > >python2的程式碼轉python3遇到的問題

python2的程式碼轉python3遇到的問題

1. SyntaxError: Missing parentheses in call to 'print’

所有“print X” 更改為“print(X)”

2. 報錯:TypeError: ‘dict_keys’ object is not subscriptable

解決:self._G.node.keys()[:] 改為 list(self._G.node.keys())

3. 報錯:AttributeError: ‘collections.defaultdict’ object has no attribute 'itervalues’

解決:m.itervalues() 改為 m.values()

4. 報錯:AttributeError: module ‘string’ has no attribute 'atoi’

解決:v_i = string.atoi(s) 改為 v_i = int(s)

5. 報錯:TypeError: ‘dict_keys’ object does not support indexing

解決:將dict_keys()型別轉化為list

visit_sequence = self._G.keys(); random.shuffle(visit_sequence) 改為 visit_sequence = list(self._G.keys()); random.shuffle(visit_sequence)