17

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?

1
  • 1
    I assume you're using Python 3. If that's correct, please add the python-3.x tag to your question. Some of the dict methods behave differently in Python 3 vs Python 2. Commented Oct 16, 2016 at 11:29

3 Answers 3

21

In Python3 dict.values() returns "views" instead of lists:

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]
Sign up to request clarification or add additional context in comments.

Comments

0

just use list(in_degrees.values()).count(x) worked for me!

Comments

-1

If you want to count dictionary values you can do it like this:

len(list(dict.values()))

same method works for keys

len(list(dict.keys()))

Also keep in mind if you want to get all keys or values in list just use list(dict.values())

1 Comment

len(mydict) is sufficient, since in this context the len of a dict is the number of keys. No need to produce a list of the values or keys. And making a copy by the use of the list operator is unnecessary as well since both mydict.keys() and mydict.values() return lists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.