Ask Your Question
0

Ordering a list of triplets according to lexical order

asked 2024-06-25 01:19:27 +0200

hamouda gravatar image

updated 2024-06-25 01:37:57 +0200

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2024-06-25 16:41:33 +0200

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)]
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

Stats

Asked: 2024-06-25 01:19:27 +0200

Seen: 115 times

Last updated: Jun 25