Sage operations that preserve order

11 views
Skip to first unread message

tvn

unread,
Jun 12, 2012, 12:56:43 PM6/12/12
to sage-s...@googlegroups.com
Does Sage has some data structure like set that preserve the order of the elements ?  For example ,  I want set([1,2,3,1) to always return 1,2,3 .  In addition to set(), I also need operations of set like difference, intersection ,etc that guarantees some specific orders.   Thanks,  

Jason Grout

unread,
Jun 12, 2012, 1:44:21 PM6/12/12
to sage-s...@googlegroups.com
set is a python datatype, so one thing to do is look for 'python ordered
set'. Indeed, one of the first links was a stack overflow question that
pointed to this:

http://code.activestate.com/recipes/576694/

Thanks,

Jason



William Stein

unread,
Jun 12, 2012, 1:47:25 PM6/12/12
to sage-s...@googlegroups.com
If you just care about order when iterating, use sorted:

for x in sorted(set([1,2,3,1])):
print x

>
> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to
> sage-support...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org



--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org
Reply all
Reply to author
Forward
0 new messages