Welcome, guest | Sign In | My Account | Store | Cart

Hopefully this function will save you the trip to oocalc/excel. Py3k code.

Python, 20 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
"""Hopefully this function will save you the trip to oocalc/excel. Py3k code.

"""

def nth(n):
    m = abs(n)
    if m % 10 < 4 and m // 10 != 1:
        return '{}{}'.format(n, ('th', 'st', 'nd', 'rd')[m % 10])
    return '{}{}'.format(n, 'th')

def rangeth(*args):
    """rangeth([start,] stop[, skip]) -> list of places (rankings)

returns a list of strings as places in a list (1st, 2nd, etc)

>>> rangeth(4)
['0th', '1st', '2nd', '3rd']

    """
    return list(map(nth, range(*args)))

I had to go out of my way at work to kick up Excel, just to use their adjacent cell completion tool to build a list of 1st, 2nd, 3rd, etc rankings. Since I went through the trouble of writing this one-off function, I went ahead and posted it here.

Can be easily re-tooled for previous versions behind Python 3.0.

Created by Andrew Yurisich on Sat, 25 Feb 2012 (MIT)
Python recipes (4591)
Andrew Yurisich's recipes (4)

Required Modules

  • (none specified)

Other Information and Tasks