Is there a need for range(len(a))?

后端 未结 11 685
执念已碎
执念已碎 2020-12-02 05:05

One frequently finds expressions of this type in python questions on SO. Either for just accessing all items of the iterable

for i in range(len(a)):
    prin         


        
11条回答
  •  萌比男神i
    2020-12-02 05:49

    Going by the comments as well as personal experience, I say no, there is no need for range(len(a)). Everything you can do with range(len(a)) can be done in another (usually far more efficient) way.

    You gave many examples in your post, so I won't repeat them here. Instead, I will give an example for those who say "What if I want just the length of a, not the items?". This is one of the only times you might consider using range(len(a)). However, even this can be done like so:

    >>> a = [1, 2, 3, 4]
    >>> for _ in a:
    ...     print True
    ...
    True
    True
    True
    True
    >>>
    

    Clements answer (as shown by Allik) can also be reworked to remove range(len(a)):

    >>> a = [6, 3, 1, 2, 5, 4]
    >>> sorted(range(len(a)), key=a.__getitem__)
    [2, 3, 1, 5, 4, 0]
    >>> # Note however that, in this case, range(len(a)) is more efficient.
    >>> [x for x, _ in sorted(enumerate(a), key=lambda i: i[1])]
    [2, 3, 1, 5, 4, 0]
    >>>
    

    So, in conclusion, range(len(a)) is not needed. Its only upside is readability (its intention is clear). But that is just preference and code style.

提交回复
热议问题