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

后端 未结 11 726
执念已碎
执念已碎 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:33

    If you have to iterate over the first len(a) items of an object b (that is larger than a), you should probably use range(len(a)):

    for i in range(len(a)):
        do_something_with(b[i])
    

提交回复
热议问题