ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 12 Jul 2014 21:42:02 +0200problem with list: how to count different elements?https://ask.sagemath.org/question/23310/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 list `K=[1,2,3,4,1,1,1]`, and we do `P=list(set(K))`, 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 list `A` obtained in my code, I get `A=[0, 1, 2, 3, 2]`. Why this doest work?
Please help!
Thanks a lotWed, 09 Jul 2014 16:47:39 +0200https://ask.sagemath.org/question/23310/problem-with-list-how-to-count-different-elements/Answer by vdelecroix for <p>Hello experts!</p>
<p>Im working with graphs.</p>
<p>My code is:</p>
<pre><code>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)
</code></pre>
<p>Doing that, <code>A</code> is a list with all the nodes connected with the node <code>2</code>, ie: <code>A=[0, 1, 2, 3, 2]</code></p>
<p>1) Why the element '2' is repeated?
2) If, instead, we use <code>A=nx.node_connected_component(G,0)</code>, we get <code>A=[0, 1, 2, 3]</code>. In this case: why now the element '0' is not repeated?
3) If we have a list <code>K=[1,2,3,4,1,1,1]</code>, and we do <code>P=list(set(K))</code>, we get <code>P=[1,2,3,4]</code> (doing that we can eliminate the repeated elements in the list). But if I do the same with the list <code>A</code> obtained in my code, I get <code>A=[0, 1, 2, 3, 2]</code>. Why this doest work?</p>
<p>Please help!</p>
<p>Thanks a lot</p>
https://ask.sagemath.org/question/23310/problem-with-list-how-to-count-different-elements/?answer=23311#post-id-23311Hi,
This is a problem with Sage integers fighting with numpy integers
sage: A[2] == A[4]
False
sage: type(A[2])
<type 'sage.rings.integer.Integer'>
sage: type(A[4])
<type 'numpy.int64'>
You can solve the issue working only with Sage integers as follows
sage: M = matrix(6)
sage: M[0,2]=M[2,0]=M[2,3]=1
sage: M[3,2]=M[2,1]=M[1,2]=1
sage: M[4,5]=M[5,4]=1
sage: G = Graph(M)
sage: A = G.connected_component_containing_vertex(2)
sage: A
[0, 1, 2, 3]
(using networx is fine as well)
There is already an old ticket related to that problem on the Sage trac server: [#13386: comparison of Sage integer with Numpy integer](http://trac.sagemath.org/ticket/13386)
EDIT: if you want to solve it on your example, use
sage: nx.node_connected_component(G, np.int(2))
[0, 1, 2, 3]
If you use only numpy and networkx, there is no need to use Sage. Just use the standard Ipython you will have less troubles.
VincentWed, 09 Jul 2014 17:24:35 +0200https://ask.sagemath.org/question/23310/problem-with-list-how-to-count-different-elements/?answer=23311#post-id-23311Comment by vdelecroix for <p>Hi,</p>
<p>This is a problem with Sage integers fighting with numpy integers</p>
<pre><code>sage: A[2] == A[4]
False
sage: type(A[2])
<type 'sage.rings.integer.Integer'>
sage: type(A[4])
<type 'numpy.int64'>
</code></pre>
<p>You can solve the issue working only with Sage integers as follows</p>
<pre><code>sage: M = matrix(6)
sage: M[0,2]=M[2,0]=M[2,3]=1
sage: M[3,2]=M[2,1]=M[1,2]=1
sage: M[4,5]=M[5,4]=1
sage: G = Graph(M)
sage: A = G.connected_component_containing_vertex(2)
sage: A
[0, 1, 2, 3]
</code></pre>
<p>(using networx is fine as well)</p>
<p>There is already an old ticket related to that problem on the Sage trac server: <a href="http://trac.sagemath.org/ticket/13386">#13386: comparison of Sage integer with Numpy integer</a></p>
<p>EDIT: if you want to solve it on your example, use</p>
<pre><code>sage: nx.node_connected_component(G, np.int(2))
[0, 1, 2, 3]
</code></pre>
<p>If you use only numpy and networkx, there is no need to use Sage. Just use the standard Ipython you will have less troubles.</p>
<p>Vincent</p>
https://ask.sagemath.org/question/23310/problem-with-list-how-to-count-different-elements/?comment=23391#post-id-23391I updated my answer accordingly. But I do not see the point of using numpy and networkx inside Sage.Sat, 12 Jul 2014 21:42:02 +0200https://ask.sagemath.org/question/23310/problem-with-list-how-to-count-different-elements/?comment=23391#post-id-23391Comment by mresimulator for <p>Hi,</p>
<p>This is a problem with Sage integers fighting with numpy integers</p>
<pre><code>sage: A[2] == A[4]
False
sage: type(A[2])
<type 'sage.rings.integer.Integer'>
sage: type(A[4])
<type 'numpy.int64'>
</code></pre>
<p>You can solve the issue working only with Sage integers as follows</p>
<pre><code>sage: M = matrix(6)
sage: M[0,2]=M[2,0]=M[2,3]=1
sage: M[3,2]=M[2,1]=M[1,2]=1
sage: M[4,5]=M[5,4]=1
sage: G = Graph(M)
sage: A = G.connected_component_containing_vertex(2)
sage: A
[0, 1, 2, 3]
</code></pre>
<p>(using networx is fine as well)</p>
<p>There is already an old ticket related to that problem on the Sage trac server: <a href="http://trac.sagemath.org/ticket/13386">#13386: comparison of Sage integer with Numpy integer</a></p>
<p>EDIT: if you want to solve it on your example, use</p>
<pre><code>sage: nx.node_connected_component(G, np.int(2))
[0, 1, 2, 3]
</code></pre>
<p>If you use only numpy and networkx, there is no need to use Sage. Just use the standard Ipython you will have less troubles.</p>
<p>Vincent</p>
https://ask.sagemath.org/question/23310/problem-with-list-how-to-count-different-elements/?comment=23360#post-id-23360Thanks a lot Vincent. The problem is that in my full-code, 'M' is an numpy array with NxN element. How can I fix my problem keeping workin this this Numpy array? ThanksFri, 11 Jul 2014 00:02:56 +0200https://ask.sagemath.org/question/23310/problem-with-list-how-to-count-different-elements/?comment=23360#post-id-23360