Python enumerate reverse index only

前提是你 提交于 2019-12-04 16:02:09

问题


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:

>> range(5)
[0, 1, 2, 3, 4]

If I enumerate this I would get the following:

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

However I want to reverse the index provided by enumerate so that I get:

[(4, 0), (3, 1), (2, 2), (1, 3), (0, 4)]

So far I have the following code:

reversed(list(enumerate(reversed(range(5)))))

I was just wondering if there was a neater way to do this?


回答1:


How about using zip instead with a reversed range?

>>> zip(range(9, -1, -1), range(10))
[(9, 0), (8, 1), (7, 2), (6, 3), (5, 4), (4, 5), (3, 6), (2, 7), (1, 8), (0, 9)]


>>> def reversedEnumerate(l):
        return zip(range(len(l)-1, -1, -1), l)
>>> reversedEnumerate(range(10))
[(9, 0), (8, 1), (7, 2), (6, 3), (5, 4), (4, 5), (3, 6), (2, 7), (1, 8), (0, 9)]

As @julienSpronk suggests, use izip to get a generator, also xrange:

import itertools
>>> import itertools
>>> def reversedEnumerate(l):
...     return itertools.izip(xrange(len(l)-1, -1, -1), l)
...     
>>> reversedEnumerate(range(10))
<itertools.izip object at 0x03749760>
>>> for i in reversedEnumerate(range(10)):
...     print i
...     
(9, 0)
(8, 1)
(7, 2)
(6, 3)
(5, 4)
(4, 5)
(3, 6)
(2, 7)
(1, 8)
(0, 9)



回答2:


I don't know if this solution is better for you, but at least it's shorter:

>>> [(4 - x, x) for x in range(5)]
[(4, 0), (3, 1), (2, 2), (1, 3), (0, 4)]



回答3:


Just take the length of your list and subtract the index from that...

L = range(5)

for i, n in L:
    my_i = len(L) -1 - i
    ...

Or if you really need a generator:

def reverse_enumerate(L):
   # Only works on things that have a len()
   l = len(L)
   for i, n in enumerate(L):
       yield l-i-1, n

enumerate() can't possibly do this, as it works with generic iterators. For instance, you can pass it infinite iterators, that don't even have a "reverse index".




回答4:


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)]




回答5:


Actually I'm using the same logic as @RemcoGerlich did, but I use list comprehension directly, which make the code now become 1-liner:

def generatelist(x):
    return [(x-1-i,n) for i,n in enumerate(range(x))]

Regarding the dilemma of choosing generator or list comprehension, here is the suggested way:

Basically, use a generator expression if all you're doing is iterating once. If you want to store and use the generated results, then you're probably better off with a list comprehension.




回答6:


If you're going to re-use it several times, you can make your own generator:

def reverse_enum(lst):
    for j, item in enumerate(lst):
        yield len(lst)-1-j, item

print list(reverse_enum(range(5)))
# [(4, 0), (3, 1), (2, 2), (1, 3), (0, 4)]

or

def reverse_enum(lst):
    return ((len(lst)-1-j, item) for j, item in enumerate(lst))



回答7:


Simply use len(lst)-i everywhere i is used. or:

[(len(range(5)) - x, x) for x in range(5)]



回答8:


Python 2

import itertools

def reversed_enumerate(seq):
    return itertools.izip(reversed(range(len(seq))), reversed(seq))

Python 3

Substitute zip for itertools.izip :)




回答9:


values = 'abcde'

for i, value in zip(reversed(range(len(values))), values):
    print(i, value)

Explanation:

values = 'abcde'

values_len = len(values) # 5
indexes = range(values_len) # [0, 1, 2, 3, 4]
reversed_indexes = reversed(indexes) # [4, 3, 2, 1, 0]

# combine reversed indexes and values
reversed_enumerator = zip(reversed_indexes, values)

for i, value in reversed_enumerator:
    print(i, value)



回答10:


We can use enumerate with len:

$ cat enumerate.py 


arr = ['stone', 'cold', 'steve', 'austin']
for i, val in enumerate(arr):
    print ("enu {} val {}".format(i, val))
for i, val in enumerate(arr):
    print ("enu {} val {}".format(len(arr) - i - 1, val))
$  python enumerate.py 
enu 0 val stone
enu 1 val cold
enu 2 val steve
enu 3 val austin
enu 3 val stone
enu 2 val cold
enu 1 val steve
enu 0 val austin
$  


来源:https://stackoverflow.com/questions/38738548/python-enumerate-reverse-index-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!