0

How to output an edge or edges from an adjacency matrix?

Hi, everyone! I've been stuck at 26.2 Practice for Python Data Structures. How to output an edge or edges from an adjacency matrix without NetworkX? A little prompt, please! Regards, Andrei x = X(5) x.add_friend(1, 3) x.add_friend(1, 5) x.add_friend(2, 5) x.add_friend(2, 4) x.add_friend(4, 5) n = int(input()) #your code goes here for row in x.adj: print() for val in row: print('{:4}'.format(val), end="")

26th Nov 2021, 4:21 PM
Andrei Shanko
Andrei Shanko - avatar
2 odpowiedzi
0
You don't need for loop just print(sum(x.adj[n-1]))
26th Nov 2021, 5:33 PM
Alexey Kopyshev
Alexey Kopyshev - avatar
0
Thanks, Alexey! The right code is: count = 0 for i in x.adj[n-1]: if i == 1: count += 1 else: pass print(count) The key point was x.adj[n-1]!!
26th Nov 2021, 6:07 PM
Andrei Shanko
Andrei Shanko - avatar