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

后端 未结 11 660
执念已碎
执念已碎 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.

    0 讨论(0)
  • 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])
    
    0 讨论(0)
  • 2020-12-02 05:36

    It's nice to have when you need to use the index for some kind of manipulation and having the current element doesn't suffice. Take for instance a binary tree that's stored in an array. If you have a method that asks you to return a list of tuples that contains each nodes direct children then you need the index.

    #0 -> 1,2 : 1 -> 3,4 : 2 -> 5,6 : 3 -> 7,8 ...
    nodes = [0,1,2,3,4,5,6,7,8,9,10]
    children = []
    for i in range(len(nodes)):
      leftNode = None
      rightNode = None
      if i*2 + 1 < len(nodes):
        leftNode = nodes[i*2 + 1]
      if i*2 + 2 < len(nodes):
        rightNode = nodes[i*2 + 2]
      children.append((leftNode,rightNode))
    return children
    

    Of course if the element you're working on is an object, you can just call a get children method. But yea, you only really need the index if you're doing some sort of manipulation.

    0 讨论(0)
  • 2020-12-02 05:40

    Short answer: mathematically speaking, no, in practical terms, yes, for example for Intentional Programming.

    Technically, the answer would be "no, it's not needed" because it's expressible using other constructs. But in practice, I use for i in range(len(a) (or for _ in range(len(a)) if I don't need the index) to make it explicit that I want to iterate as many times as there are items in a sequence without needing to use the items in the sequence for anything.

    So: "Is there a need?"? — yes, I need it to express the meaning/intent of the code for readability purposes.

    See also: https://en.wikipedia.org/wiki/Intentional_programming

    And obviously, if there is no collection that is associated with the iteration at all, for ... in range(len(N)) is the only option, so as to not resort to i = 0; while i < N; i += 1 ...

    0 讨论(0)
  • 2020-12-02 05:41

    What if you need to access two elements of the list simultaneously?

    for i in range(len(a[0:-1])):
        something_new[i] = a[i] * a[i+1]
    

    You can use this, but it's probably less clear:

    for i, _ in enumerate(a[0:-1]):
         something_new[i] = a[i] * a[i+1]
    

    Personally I'm not 100% happy with either!

    0 讨论(0)
  • 2020-12-02 05:44

    My code is:

    s=["9"]*int(input())
    for I in range(len(s)):
        while not set(s[I])<=set('01'):s[i]=input(i)
    print(bin(sum([int(x,2)for x in s]))[2:])
    

    It is a binary adder but I don't think the range len or the inside can be replaced to make it smaller/better.

    0 讨论(0)
提交回复
热议问题