vicR
vicR

Reputation: 819

Python get degrees within nodes

How can I get the degrees from a network graph.

list(graph.degree().values())

example [0,2,4,1,0,3,2,1,4,0,2,0] and I want just the degrees like.

[0,1,2,3,4]

is there some kind of function within python or networkx

Upvotes: 0

Views: 1533

Answers (1)

Haifeng Zhang
Haifeng Zhang

Reputation: 31903

If you want to only keep unique elements in your list,

Use set()

mylist = list(graph.degree().values())
unique_elements_list = list(set(mylist))

Use list():

mylist = list(graph.degree().values())

unique_elements_list = list()

# iterates all elements in mylist
for element in mylist:
    # adds to unique elements list only current
    # element is not included.
    if element not in unique_elements_list:
        unique_elements_list.append(element)

Upvotes: 1

Related Questions