Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

problem with list: how to count different elements?

Hello experts!

Im working with graphs.

My code is:

import numpy as np
import networkx as nx

M = np.zeros([6,6])

M[0,2]=1
M[2,0]=1
M[2,3]=1
M[3,2]=1
M[2,1]=1
M[1,2]=1
M[4,5]=1
M[5,4]=1

G=nx.Graph(M)

A=nx.node_connected_component(G,2)

Doing that, A is a list with all the nodes connected with the node 2, ie: A=[0, 1, 2, 3, 2]

1) Why the element '2' is repeated? 2) If, instead, we use A=nx.node_connected_component(G,0), we get A=[0, 1, 2, 3]. In this case: why now the element '0' is not repeated? 3) If we have a listK=[1,2,3,4,1,1,1], and we doP=list(set(K)), we getP=[1,2,3,4](doing that we can eliminate the repeated elements in the list). But if I do the same with the listAobtained in my code, I getA=[0, 1, 2, 3, 2]`. Why this doest work?

Please help!

Thanks a lot

problem with list: how to count different elements?

Hello experts!

Im working with graphs.

My code is:

import numpy as np
import networkx as nx

M = np.zeros([6,6])

M[0,2]=1
M[2,0]=1
M[2,3]=1
M[3,2]=1
M[2,1]=1
M[1,2]=1
M[4,5]=1
M[5,4]=1

G=nx.Graph(M)

A=nx.node_connected_component(G,2)

Doing that, A is a list with all the nodes connected with the node 2, ie: A=[0, 1, 2, 3, 2]

1) Why the element '2' is repeated? 2) If, instead, we use A=nx.node_connected_component(G,0), we get A=[0, 1, 2, 3]. In this case: why now the element '0' is not repeated? 3) If we have a listlist K=[1,2,3,4,1,1,1], and we dodo P=list(set(K)), we get, we get P=[1,2,3,4] (doing that we can eliminate the repeated elements in the list). But if I do the same with the listlist A obtained in my code, I getget A=[0, 1, 2, 3, 2]`. 2]. Why this doest work?

Please help!

Thanks a lot