Average of 2 values followed by a list

Asked

Viewed 34 times

1

I have one question: I’m trying to solve an exercise for learning, which is the following:

inform a list. Ex: [1, 3, 5, 1, -10], and return the average between the number and the next number. In this example, you should return: [2, 4, 3, -4.5]. I have no idea how to "slice" a list, because it has no limit, could create a list with 100 values...

1 answer

1


As you are using a list of indefinite size just iterate with a while making the average of the current position + the next position. The loop closes when it reaches the position of tamanho da sua lista -1.

For example:

lista = [1, 3, 5, 1, -10]

tamanho = len(lista)
pos = 0
while pos < tamanho-1:
    print((lista[pos]+lista[pos+1])/2)
    pos+=1

exit:

2.0

4.0

3.0

-4.5

Or, as you said in the comments, control can be done by deleting the first item from the list. In this case the while will close when the list has less than 2 items.

lista = [1, 3, 5, 1, -10]

pos = 0
while len(lista) >= 2:
    print((lista[pos]+lista[pos+1])/2)
    lista.pop(0)
  • Pow, show me a show! I was trying to do a little different, because since I’m a beginner, I don’t understand very well, so I was always trying to get the Indice[0] and the Indice[1], average the 2, and then remove the Indice[0], moving your place to the next number, would it work?

  • And another, why did you use pos < size-1? This -1 is doing exactly what?

  • It works yes, I added a second version with this approach.

  • The pos < tamanho - 1 is the control of your list. As you need 2 elements to have the average pos will increase with each loop: it starts with 0, then 1, 2.... The noose while it ceases to be executed when pos is larger than the total size of the list -1, that is, if the list has 10 items it will execute up to pos = 9, so that it does not arrive at loop 10 and has no one to average and ends up generating an error (position 11 of the list does not exist).

  • Ah, got it, Max! Man, thank you so much for clearing up my doubts! I’m entering this world of programming, next year I start college, and I want to get ready now, you helped me understand something very show. Thank you!

  • Show man, don’t give up and study hard. And the best way to learn is teaching. So if I helped you and you were grateful, help someone when you have the chance. That is why the community exists =) Ahhh, and if you can vote in favour of my answer I would also be happy. Hugs

  • I will help yes. I am trying to find where to vote in your reply hahaha

  • Up there, where you’ve marked the approved response, there’s a little fledgling up and down. Click the button up whenever any answers are helpful and help you solve a problem.

  • All right, like Max!

  • It would be nice if you took a tour of the https://answall.com/tour. And a final tip: Study with the Guanabara language course on Youtube, you’ll evolve a lot, from basic to intermediate

  • I don’t have a 15, apparently you haven’t counted my vote in your question yet, but as soon as I get to 15, I come back here and leave the vote! Okay, I’m gonna take this tour, and watch the Guanabara course, thanks for the tips.

  • Show. Abração e bons estudos https://www.youtube.com/playlist?list=PLHz_AreHm4dlKP6QQCekuIPky1CiwmdI6

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.