我该如何修复AttributeError:' dict_values'对象没有属性' count'?

时间:2016-10-16 11:09:39

标签: python python-3.x networkx

此处是我的code,文本文件为here

<bean id="sessionFactory" 
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    ...
</bean>

我想绘制学位分布网络图 我怎样才能改变dict来解决?

4 个答案:

答案 0 :(得分:6)

在Python3 dict.values()中返回&#34; views&#34;而不是列表:

  

转换&#34;视图&#34;在列表中,只需将in_degrees.values()包裹在list()

in_hist = [list(in_degrees.values()).count(x) for x in in_values]

答案 1 :(得分:0)

错误很明显dict_values对象没有属性count。如果您想计算唯一值的数量,最好的方法是使用collections.Counter

from collections import Counter
in_hist = Counter(in_degrees.values())

答案 2 :(得分:0)

只需使用 list(in_degrees.values()).count(x) 为我工作!

答案 3 :(得分:-1)

如果你想计算字典值,你可以这样做:

len(list(dict.values()))

相同的方法适用于键

len(list(dict.keys()))

另请注意,如果您想要获取列表中的所有键或值,请使用list(dict.values())