Iterate indexes of a vector

Asked

Viewed 42 times

1

I have the following vector:

a = [10, 20, 30, 40...]

I need to create a new list that is the subtraction of the second index by the first, the third index by the first and so on... Then the third index by the second, fourth index by the third and so on. For example:

b = [índice[1] - índice[0], índice[2] - índice[0], índice[3] - índice[0], índice[2] - índice[1], índice[3] - índice[1]...]

That is, I need to iterate all conditions, but always in this logic the subsequent index less the previous one.

I have the script below, but it does not iterate all conditions.

a = [10, 50, 30, 5]
b = [a[i+1]-a[i] for i in range(0, len(a)) if i+1 < len(a)]
print (b)

I’m sorry, in case I haven’t been very clear. I tried to be awesome!

Someone could help me?

  • It is not very clear which indexes will be subtracted. Do you need to subtract 0 from all elements i > 0, subtract 1 from all elements i > 1, subtract 2 from all elements i > 2, etc.? And what exactly is the difference to this question (https://answall.com/q/230085/5878)?

  • That’s right! What I need. The difference to this quoted question is that I am making the Indice[1] - Indice[0], then index[4]-index[3]. I need it to index[1]- index[0], then index[2] - index[0] and index[3]-index[0]. That is, it is as if I were to catch the index[0] and perform the subtraction procedures. When it reached its end, it jumped to the index[1], and did the same with the index[2] and index[3] and so on. I was able to explain it to you better? I’m sorry for anything.

2 answers

2


Then to get the desired result, you will need two loops: one to go through the index that will be subtracted and the other the indices that will subtract. That is, the first part of the list will be all elements subtracting element 0, the second part will be all elements greater than 1 subtracting element 1, etc. The first loop defines the value x = 0, 1, 2, ..., n; the second will be y = x+1, X+2, ..., n, the value of a[y]-a[x]. Behold:

a = [3, 5, 2]

resultado = []

for ix, x in enumerate(a):
    for y in a[ix+1:]:
        resultado.append(y - x)

print resultado

See working on Ideone.

The result will be [2, -1, -3], as it relates to a[1]-a[0], a[2]-a[0], a[2]-a[1].


Correction: it is necessary to use the function enumerate to iterate over the elements of the list, as it will return a pair of values referring to the index and the value itself. Before it was just using the value, which generated the unexpected behavior commented.

  • Thank you @Andersoncarlos Woss. I believe that with this I can continue in the stage I am in my software.

  • I just realized, but when the numbers on my list result in a negative value, for example (3-4), they are not displayed in the list. I ran a test with these values a = [3, 5, 2] and it showed nothing. I realize that it works correctly when the result of my subtraction is a positive value. But when the result is negative, it doesn’t work. You could tell me how to fix this?

  • 1

    @Danilo, ready, I believe the problem has been solved.

  • Thanks @Anderson Carlos Woss. Really it’s all OK now!

1

Follow an example:

a = [10, 20, 50, 80, 50, 60];
b = [];

tamanhoLista = len(a); 

for i in range(tamanhoLista):
    subtracoes = [];
    for x in range(i, tamanhoLista):
        if (x+1) < tamanhoLista:
          subtracoes.append(a[x + 1]-a[i]);
        elif (i == (tamanhoLista - 1)):
            subtracoes.append(a[x]-a[i]);
    b.append(subtracoes);

print(b);

The return is:

[
    [10, 40, 70, 40, 50], 
    [30, 60, 30, 40], 
    [30, 0, 10], 
    [-30, -20], 
    [10], 
    [0]
]
  • Thank you very much! It also works.

Browser other questions tagged

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