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
  • 13,387
  • 4
  • 15
  • 42
zxzx179
  • 187
  • 6
  • `(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
  • 7,193
  • 5
  • 23
  • 40
Tim Roberts
  • 34,376
  • 3
  • 17
  • 24