Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a library to convert integer to first/second/third [closed]

Tags:

I have a list of integers - is there a library to convert them into plain text ranking? IE: 1,2,3 -> "first, second, third" ?

like image 715
gone Avatar asked Jun 24 '12 20:06

gone


2 Answers

The python inflect package has a method for converting numerals into ordinals:

import inflect p = inflect.engine()  for i in range(1,25,5):     print(p.ordinal(i)) 

displays:

1st 6th 11th 16th 21st 
like image 114
psychemedia Avatar answered Oct 05 '22 23:10

psychemedia


How high are you planning on going? (Do you ever expect higher than, say, "twentieth"?)

Maybe you just need a dict,

nth = {     1: "first",     2: "second",     3: "third",     4: "fourth"     # etc } 
like image 26
Hugh Bothwell Avatar answered Oct 06 '22 00:10

Hugh Bothwell