Python enumerate reverse index only

前端 未结 10 2244
情深已故
情深已故 2021-02-05 05:43

I am trying to reverse the index given by enumerate whilst retaining the original order of the list being enumerated.

Assume I have the following:



        
10条回答
  •  广开言路
    2021-02-05 06:45

    Assuming your list is not long and you will not run into performance errors, you may use list(enumerate(range(5)[::-1]))[::-1].

    Test:

    >>> list(enumerate(range(5)[::-1]))[::-1] [(0, 4), (1, 3), (2, 2), (3, 1), (4, 0)]

提交回复
热议问题