开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜