Inserting information several times

Asked

Viewed 49 times

-1

Good afternoon, I am developing a small script to organize some information that are contained in a vector, the idea is to go through two vectors, when the information of each is equal, must enter a \n and then the element of VetorUm. The VetorUm is the main, the VetorDois is just information to help put the \n in the right place and if the elements are different you should only insert the element, but I do not know where I was wrong since you are inserting the elements several times.

Follows the code:

for indice , item in enumerate(VetorUm):
    for Contador in range(0, len(VetorDois)):
        if item == NomePlacas[Contador]:
            ResultadoSaida.insert(indice,"\n" + "\n")
        ResultadoSaida.append(item)

Example of vectors:

VetorUm = ['este','é','um','vetor','para','exemplo','do','problema','apresentado','na','questão','acima']

VetorDois = ['vetor','problema','questão']

ResultadoSaida = ['este','é','um','\n','vetor','para','exemplo','do','\n','problema','apresentado','na','\n','questão','acima']
  • And what is "more or less wrong" in your opinion? Get [Edit] the question and add examples of what the VetorUm and VetorDois and what should be the output generated?

  • It is that it is inserting information every time it passes in Vector 2, this I think is the error, I will edit.

  • Gabriel, for the example he posed in the question, which should be the way out?

  • The output would be the vector.

  • The order of the words in VetorDois does it matter? For example, if in VetorUm appear the word 'problema' before the word 'vetor' the break should be inserted or not? That is, it should insert because the word percence to the VetorDois or should not be inserted because the word 'vetor'?

  • The order does not matter, because the content will vary a lot. Whenever these words appear, it has to happen, no matter where they are and how often they appear.

Show 1 more comment

2 answers

0


vetorUm = ['este','é','um','vetor','para','exemplo','do','problema','apresentado','na','questão','acima']
vetorDois = ['vetor','problema','questão']
vetorResultado = []

for item1 in vetorUm:
    if item1 in vetorDois:
        vetorResultado.append('\n')
    vetorResultado.append(item1)
print(vetorResultado)
  • It worked perfectly, thank you very much.

0

The idea is to always add a line break, '\n', in the output list when a word of VetorUm that belongs to the VetorDois. So just make a loop through VetorUm and check if the word is in VetorDois:

vetor_um = ['este','é','um','vetor','para','exemplo','do','problema','apresentado','na','questão','acima']
vetor_dois = ['vetor','problema','questão']
resultado = []

for palavra in vetor_um:
  if palavra in vetor_dois:
    resultado.append('\n')
  resultado.append(palavra)

print(resultado)

The exit will be: ['este', 'é', 'um', '\n', 'vetor', 'para', 'exemplo', 'do', '\n', 'problema', 'apresentado', 'na', '\n', 'questão', 'acima']

You could even define a function that works with generators:

def adiciona_quebra_linhas(vetor_um, vetor_dois):
  for palavra in vetor_um:
    if palavra in vetor_dois:
      yield '\n'
    yield palavra

See working on Repl.it

And so to call adiciona_quebra_linhas(vetor_um, vetor_dois), obtaining the same result.

Browser other questions tagged

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