Python Data structure index Start at 1 instead of 0?

前端 未结 4 737
说谎
说谎 2020-12-19 02:06

I have a weird question: I have this list of 64 numbers that will never change:

(2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40,          


        
4条回答
  •  心在旅途
    2020-12-19 03:03

    You could use range(2, 129, 2) to generate the numbers in the range 1 - 128 in increments of 2 and convert this list into a tuple if it's not going to change.

    t = tuple(range(2, 129, 2))
    
    def numbers(n):
       return t[n-1]
    

    Given the global tuple t, function numbers could retrieve elements using a 1-based (instead of 0-based) index.

提交回复
热议问题