Ask Your Question
0

Ordering a list of triplets according to lexical order

asked 0 years ago

hamouda gravatar image

updated 0 years ago

Given two triplets (a,b,c) and (a',b',c'), by definition (a,b,c) is less than (a',b',c') with respect to lexical order if a < a' or (a=a' and b < b') or (a=a' and b=b' and c < c' ). I want to order a list of triplets according to such order, for example if a list L consists of (1,2,3), (1,1,5), (1,3,5), (1,6,7), (8,7,3) . Is there a technique to do this by Sage.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 0 years ago

Max Alekseyev gravatar image

Lexicographic order is the default one for tuples. So, you can simply use sorted() function or .sort() method on your list of tuples:

sage: L = [(1,2,3), (1,1,5), (1,3,5), (1,6,7), (8,7,3)]
sage: print( sorted(L) )
[(1, 1, 5), (1, 2, 3), (1, 3, 5), (1, 6, 7), (8, 7, 3)]
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: 0 years ago

Seen: 203 times

Last updated: Jun 25 '24