Traverse a vector to a given number of indexes and form a list of values

Asked

Viewed 476 times

0

I have the following vector:

a = [10, 10, 10, 10, 10, 20, 20, 20, 20, 20]

I need every five indexes to form a list of these elements.

You’d look like this:

b = [(10, 10, 10, 10, 10), (20, 20, 20, 20, 20)]

I believe it is something simple, but I’m not able to execute.

Could someone help me?

  • Leave the code for us to see

  • I have no code, only the indicated vector a = [10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20]

1 answer

1


An alternative, see working here.

a = [10, 10, 10, 10, 10, 20, 20, 20, 20, 20]
b = []
t =[] # Temporario

i = 1
for numero in a:
  t.append(numero)
  if i == 5:
    i = 0 # Volta a contagem do zero
    b.append(tuple(t))
    t = [] # Limpa
  i += 1 # Incrementa +1

print(b)
  • Thank you very much! That’s what I need.

Browser other questions tagged

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