Python Data structure index Start at 1 instead of 0?

前端 未结 4 748
说谎
说谎 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 02:42

    You could use a dictionary, or you could simply subtract one from your index before accessing it.

    Also, I note that your 64 numbers are in a simple arithmetic progression. Why store them at all? You can use this:

    def my_number(i):
        return 2*i
    

    If the list you showed was actually an example, and the real numbers are more complicated, then use a list with a dummy first element:

    my_nums = [0, 2, 4, 6, 8, ....]
    

    Then you can get 2 as my_nums[1].

提交回复
热议问题