Ask Your Question
1

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

asked 7 years ago

Peter Heinig gravatar image

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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 7 years ago

tmonteil gravatar image

updated 7 years ago

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: [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)
Preview: (hide)
link

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

Stats

Asked: 7 years ago

Seen: 279 times

Last updated: Mar 08 '18