我该如何修复AttributeError:'dict_values'对象没有属性'count'?
问题描述:
这是我的代码,文本文件是
here is my code and the text file is here
import networkx as nx
import pylab as plt
webg = nx.read_edgelist('web-graph.txt',create_using=nx.DiGraph(),nodetype=int)
in_degrees = webg.in_degree()
in_values = sorted(set(in_degrees.values()))
in_hist = [in_degrees.values().count(x)for x in in_values]
我想绘制度分布网络图 我该如何更改字典来解决?
I want to plot degree distribution web graph how can i change dict to solve?
答
在Python3中,dict.values()
返回视图"而不是列表:
In Python3 dict.values()
returns "views" instead of lists:
- dict方法dict.keys(),dict.items()和dict.values()返回视图"而不是列表. https://docs.python.org/3/whatsnew/3.0.html
- dict methods dict.keys(), dict.items() and dict.values() return "views" instead of lists. https://docs.python.org/3/whatsnew/3.0.html
要将视图"转换为列表,只需将in_degrees.values()
包裹在list()
中:
To convert the "view" into a list, simply wrap in_degrees.values()
in a list()
:
in_hist = [list(in_degrees.values()).count(x) for x in in_values]