How can I iterate over the element of a set?

32 views
Skip to first unread message

Peder Jakobsen

unread,
Oct 29, 2014, 8:42:29 PM10/29/14
to sy...@googlegroups.com
from sympy import *
H,B = symbols('H, B')
H = FiniteSet('A', 'B', 'C', 'E', 'F')
B = FiniteSet('Joe', 'Jack', 'Sue', 'Jill')
N = Union(H,B)

# how would I do something like this?

for n in N:
    #perform some operation
    foo(n)

Addendum:
In my case, I'd actually like to iterate over the set to create nodes and vertices in NetworkX

import networkx as nx

G = nx.DiGraph()
G.add_node("A")
G.add_node("B")
etc. 

G.add_edge("A", "B")
etc. 

# Then I can draw it in iPython notebook
nx.draw(G, node_size=90, node_color='Red') 





Aaron Meurer

unread,
Oct 31, 2014, 12:21:55 PM10/31/14
to sy...@googlegroups.com
On Wed, Oct 29, 2014 at 7:42 PM, Peder Jakobsen <pjak...@gmail.com> wrote:
> from sympy import *
> H,B = symbols('H, B')
> H = FiniteSet('A', 'B', 'C', 'E', 'F')
> B = FiniteSet('Joe', 'Jack', 'Sue', 'Jill')
> N = Union(H,B)
>
> # how would I do something like this?
>
> for n in N:
> #perform some operation
> foo(n)

It looks like that works.

Aaron Meurer

>
> Addendum:
> In my case, I'd actually like to iterate over the set to create nodes and
> vertices in NetworkX
>
> import networkx as nx
>
> G = nx.DiGraph()
> G.add_node("A")
> G.add_node("B")
> etc.
>
> G.add_edge("A", "B")
> etc.
>
> # Then I can draw it in iPython notebook
> nx.draw(G, node_size=90, node_color='Red')
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/dbc6b35d-d2d8-4d96-8032-d66909760ecd%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages