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

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

    I have an use case I don't believe any of your examples cover.

    boxes = [b1, b2, b3]
    items = [i1, i2, i3, i4, i5]
    for j in range(len(boxes)):
        boxes[j].putitemin(items[j])
    

    I'm relatively new to python though so happy to learn a more elegant approach.

提交回复
热议问题