Ask Your Question

How to take an initial segment of the data structure graphs(n) ?

asked 2018-03-08 02:27:10 -0500

Peter Heinig gravatar image

Is there a possibility to access individual elements of the data-structure constructed by the command


? 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.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-03-08 04:21:25 -0500

tmonteil gravatar image

updated 2018-03-08 04:22:12 -0500

Such 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: [ 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)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-03-08 02:27:10 -0500

Seen: 12 times

Last updated: Mar 08