ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 08 Mar 2018 04:21:25 -0600How to take an initial segment of the data structure graphs(n) ?http://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 02:27:10 -0600http://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>
http://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 04:21:25 -0600http://ask.sagemath.org/question/41432/how-to-take-an-initial-segment-of-the-data-structure-graphsn/?answer=41434#post-id-41434