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

后端 未结 11 723
执念已碎
执念已碎 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条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 05:49

    If you need to work with indices of a sequence, then yes - you use it... eg for the equivalent of numpy.argsort...:

    >>> a = [6, 3, 1, 2, 5, 4]
    >>> sorted(range(len(a)), key=a.__getitem__)
    [2, 3, 1, 5, 4, 0]
    

提交回复
热议问题