finding a list item by its index
I've started off with this.
def month(n):
lst = ['Months','Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
lst.inde开发者_JAVA百科x(x)
I need it to work as follows:
>>>first = month(1) >>>first 'Jan' >>> second = month(11) >>> second 'Nov'
How can this be done?
def month(n):
lst = ['Months','Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
return lst[n]
Why not use a dictionary?
lst = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
months = dict(zip(range(1, 13), lst))
month = months.get
month(1) # Jan
精彩评论