Interfacing of vectors in python

Asked

Viewed 415 times

1

Make a program that fills two vectors of five numerical elements each and shows the vector resulting from their intercalation. Vectors must be initialized with the values below:

https://photos.app.goo.gl/egHj5dYLAXxfbLoW6

First I decided to make a smaller version, where the third vector will have only 5 elements. I got it here(in case the view is confused, the code is more organized in Google Docs: https://docs.google.com/document/d/13poguwUMA4y3mumadOkDJgarwzqx_vr-HkZgw9MMmg4/edit?usp=drivesdk):

vetor1 = [0]*5
vetor1[0] = 4
vetor1[1] = -9
vetor1[2] = 78
vetor1[3] = 0
vetor1[4] = 25
print(vetor1)

vetor2 = [0]*5
vetor2[0] = 8
vetor2[1]= 2
vetor2[2] = 34
vetor2[3] = 90
vetor2[4]= 200
print(vetor2)

v3 = [0] * 5

for i in range(5):
    if i%2 == 0:
        v3[i]  = vetor1[i/2]
    else:

And this is where I start to have problems. I can’t find a formula to relate all v3 and v2 indexes.

3 answers

3

A trivial way to solve this is by using the structure itertools.chain together with the zip:

from itertools import chain

a = [4, -9, 78, 0, 25]                                                                          
b = [8, 2, 34, 90, 200]

resultado = chain.from_iterable(zip(a, b))

print(*resultado)
# 4 8 -9 2 78 34 0 90 25 200

The class zip will generate an iterable object that will produce tuples with the two values respective to each position of the lists and the function chain.from_iterable will flatten this structure with all values on the same level. The return is a generator, as it was necessary to the * to consume it and display the values.

The equivalent Python code for this chunk would be:

def intercalar(a, b):
  for i, j in zip(a, b):
    yield i
    yield j

Or more generally, without limiting it to two lists:

def intercalar(*listas):
  for valores in zip(*listas):
    for valor in valores:
      yield valor

Or else:

def intercalar(*listas):
  for valores in zip(*listas):
    yield from valores

3

Using lists in comprehension (namely [ x | par ∈ zip(a,b) ∧ x ∈ par ]):

a=[4, -9, 78, 0, 25]                                                                          
b=[8, 2, 34, 90, 200] 
print([x for par in zip(a,b) for x in par])

Exit:

[4, 8, -9, 2, 78, 34, 0, 90, 25, 200]
  • 1

    Much more pythonica! + 1

  • I did not understand the functioning of the structure " x for par in zip(a,b)"

  • @Pabloleonardo in mathematical language, this is usually written as [ x | par ∈ zip(a,b) ∧ x ∈ par ]

2

I did not understand why to use a third vector, I believe it is to assign the values of vetor1 and vetor2, correct? If yes, a new vector is not required. To perform such intercalation we use the method zip().

Your code will look like this:

vetor1 = [0]*5
vetor1[0] = 4
vetor1[1] = -9
vetor1[2] = 78
vetor1[3] = 0
vetor1[4] = 25
print(vetor1)

vetor2 = [0]*5
vetor2[0] = 8
vetor2[1]= 2
vetor2[2] = 34
vetor2[3] = 90
vetor2[4]= 200
print(vetor2)

v3 = [0] * 5

def zipar(v1,v2):
    zipar = []
    for x,y in zip(v1, v2):
        zipar.append(x)
        zipar.append(y)
    return zipar



ListaZipada = zipar(vetor1, vetor2)

for i in ListaZipada:

    print(i ,end=" " )

Exit:

[4, -9, 78, 0, 25]                                                                          
[8, 2, 34, 90, 200]                                                                         
4 8 -9 2 78 34 0 90 25 200

I used as a base this reply, recommend reading it and the documentation on the method zip()

Note that there was a change in the last print(i ,end=" " ) to get the output in a single line.

I recommend reading: There is a way to print everything without line break?

  • I was confused by the need for Return. I removed it from the code and found that the compiler accuses that "zipping" is not an eternal object. So at least now I understand what Return does there, but I don’t understand why it zips an eternal object.

Browser other questions tagged

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