1

I am learning the networkx library and I came across this (source code is here)

sorted(d for n, d in G.degree())

which sorts the degrees of nodes in graph G. I don't quite understand this, especially d for n: what does n mean here?

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
zxzx179
  • 187
  • 7
  • `(n,d)` is a tuple. So `d` is its second element. – PM 77-1 Apr 15 '21 at 20:14
  • 1
    To be clear, this is a generator expression, not a list comprehension, as described in https://stackoverflow.com/questions/47789/generator-expressions-vs-list-comprehensions – Craig Apr 15 '21 at 20:16

1 Answers1

1

This is shorthand for

lst = []
for n, d in G.degree():
   lst.append(d)
lst.sort()

So, G.degree is expected to return a set of tuples with two elements, where the second one has degrees. You're just collecting those values and sorting them.

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30