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.Thu, 08 Mar 2018 11:21:25 +0100How to take an initial segment of the data structure graphs(n) ?https://ask.sagemath.org/question/41432/how-to-take-an-initial-segment-of-the-data-structure-graphsn/ Is there a possibility to access individual elements of the data-structure constructed by the command
> graphs(7)
? How to take an initial segment of it?
It seems not to be a list, which presumably is the reason why commands like ' graphs(7)[0] ' or ' graphs(7)[:5] ' are not allowed. Thu, 08 Mar 2018 09:27:10 +0100https://ask.sagemath.org/question/41432/how-to-take-an-initial-segment-of-the-data-structure-graphsn/Answer by tmonteil for <p>Is there a possibility to access individual elements of the data-structure constructed by the command</p>
<blockquote>
<p>graphs(7)</p>
</blockquote>
<p>? How to take an initial segment of it?</p>
<p>It seems not to be a list, which presumably is the reason why commands like ' graphs(7)[0] ' or ' graphs(7)[:5] ' are not allowed. </p>
https://ask.sagemath.org/question/41432/how-to-take-an-initial-segment-of-the-data-structure-graphsn/?answer=41434#post-id-41434Such object is called an *iterator* in the Python world, you will find tons of nice tutorials with examples on the web. For example, if you want a list containing the first 10 elements generated by it, just do:
sage: G = graphs(7)
sage: [G.next() for _ in range(10)]
If you want to iterate over its elements until you find an interesting one, you can do something like:
sage: for g in graphs(7):
....: if very_interesting_property(g):
....: do_something_interesting(g)
You can also produce an iterator selectinf the interesting ones:
sage: I = (g for g in graphs(7) if very_interesting_property(g))
and then do:
sage: for g in I:
....: do_something_interesting(g)Thu, 08 Mar 2018 11:21:25 +0100https://ask.sagemath.org/question/41432/how-to-take-an-initial-segment-of-the-data-structure-graphsn/?answer=41434#post-id-41434