Ask Your Question

Revision history [back]

Using a lambda expression in digraphs fails for len(G.sinks())?

I'm gathering digraphs that have exactly one sink. This list comprehension works fine to find the 6 such digraphs of order 3 & put them in a list:

[G for G in digraphs(3) if len(G.sinks()) == 1]

However, I need this to work in a lambda expression to make it part of a larger calculation. However,

digraphs(3,lambda G: len(G.sinks()) == 1)

creates an iterator that returns no entries. Creating a lamba expression creates the same unexpected zero-length iterator in digraphs, but that same iterator correctly finds the 6 single-sink digraphs if used in a for expression:

sage:     property = lambda G: len(G.sinks()) == 1
sage:     T = digraphs(3, property)
sage:     print(list(T))
[]
sage: for H in digraphs(3):
....:     print property(H)
....:     
False
False
False
True
False
False
False
False
True
True
True
True
False
True
False
False

So, what's the right way to generate the one-sink digraphs of order N directly in the digraphs() generator?

click to hide/show revision 2
retagged

Using a lambda expression in digraphs fails for len(G.sinks())?

I'm gathering digraphs that have exactly one sink. This list comprehension works fine to find the 6 such digraphs of order 3 & put them in a list:

[G for G in digraphs(3) if len(G.sinks()) == 1]

However, I need this to work in a lambda expression to make it part of a larger calculation. However,

digraphs(3,lambda G: len(G.sinks()) == 1)

creates an iterator that returns no entries. Creating a lamba expression creates the same unexpected zero-length iterator in digraphs, but that same iterator correctly finds the 6 single-sink digraphs if used in a for expression:

sage:     property = lambda G: len(G.sinks()) == 1
sage:     T = digraphs(3, property)
sage:     print(list(T))
[]
sage: for H in digraphs(3):
....:     print property(H)
....:     
False
False
False
True
False
False
False
False
True
True
True
True
False
True
False
False

So, what's the right way to generate the one-sink digraphs of order N directly in the digraphs() generator?

Using a lambda expression in digraphs fails for len(G.sinks())?

I'm gathering digraphs that have exactly one sink. This list comprehension works fine to find the 6 such digraphs of order 3 & put them in a list:

[G for G in digraphs(3) if len(G.sinks()) == 1]

However, I need this to work in a lambda expression to make it part of a larger calculation. However,

digraphs(3,lambda G: len(G.sinks()) == 1)

creates an iterator that returns no entries. Creating a lamba expression creates the same unexpected zero-length iterator in digraphs, but that same iterator correctly finds the 6 single-sink digraphs if used in a for expression:

sage:     property = lambda G: len(G.sinks()) == 1
sage:     T = digraphs(3, property)
sage:     print(list(T))
[]
sage: for H in digraphs(3):
....:     print property(H)
....:     
False
False
False
True
False
False
False
False
True
True
True
True
False
True
False
False

So, what's the right way to generate the one-sink digraphs of order N directly in the digraphs() generator?