Python Loop: List Index Out of Range

前端 未结 4 1009
情话喂你
情话喂你 2020-11-28 15:10

Given the following list

a = [0, 1, 2, 3]

I\'d like to create a new list b, which consists of elements for which the current a

相关标签:
4条回答
  • 2020-11-28 15:41

    Try reducing the range of the for loop to range(len(a)-1):

    a = [0,1,2,3]
    b = []
    
    for i in range(len(a)-1):
        b.append(a[i]+a[i+1])
    
    print(b)
    

    This can also be written as a list comprehension:

    b = [a[i] + a[i+1] for i in range(len(a)-1)]
    print(b)
    
    0 讨论(0)
  • 2020-11-28 15:43
    1. In your for loop, you're iterating through the elements of a list a. But in the body of the loop, you're using those items to index that list, when you actually want indexes.
      Imagine if the list a would contain 5 items, a number 100 would be among them and the for loop would reach it. You will essentially attempt to retrieve the 100th element of the list a, which obviously is not there. This will give you an IndexError.

    We can fix this issue by iterating over a range of indexes instead:

    for i in range(len(a))
    

    and access the a's items like that: a[i]. This won't give any errors.

    1. In the loop's body, you're indexing not only a[i], but also a[i+1]. This is also a place for a potential error. If your list contains 5 items and you're iterating over it like I've shown in the point 1, you'll get an IndexError. Why? Because range(5) is essentially 0 1 2 3 4, so when the loop reaches 4, you will attempt to get the a[5] item. Since indexing in Python starts with 0 and your list contains 5 items, the last item would have an index 4, so getting the a[5] would mean getting the sixth element which does not exist.

    To fix that, you should subtract 1 from len(a) in order to get a range sequence 0 1 2 3. Since you're using an index i+1, you'll still get the last element, but this way you will avoid the error.

    1. There are many different ways to accomplish what you're trying to do here. Some of them are quite elegant and more "pythonic", like list comprehensions:

    b = [a[i] + a[i+1] for i in range(len(a) - 1)]

    This does the job in only one line.

    0 讨论(0)
  • 2020-11-28 15:43

    When you call for i in a:, you are getting the actual elements, not the indexes. When we reach the last element, that is 3, b.append(a[i+1]-a[i]) looks for a[4], doesn't find one and then fails. Instead, try iterating over the indexes while stopping just short of the last one, like

    for i in range(0, len(a)-1): Do something

    Your current code won't work yet for the do something part though ;)

    0 讨论(0)
  • 2020-11-28 15:48

    You are accessing the list elements and then using them to attempt to index your list. This is not a good idea. You already have an answer showing how you could use indexing to get your sum list, but another option would be to zip the list with a slice of itself such that you can sum the pairs.

    b = [i + j for i, j in zip(a, a[1:])]
    
    0 讨论(0)
提交回复
热议问题