开发者

finding a list item by its index

开发者 https://www.devze.com 2023-02-07 03:04 出处:网络
I\'ve started off with this. def month(n): lst = [\'Months\',\'Jan\', \'Feb\', \'Mar\', \'Apr\', \'May\', \'Jun\', \'Jul\', \'Aug\', \'Sep\', \'Oct\', \'Nov\', \'Dec\']

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

精彩评论

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

关注公众号