python vectors, help out?

Asked

Viewed 104 times

-3

I don’t know where I’m going wrong,.

Make a program that reads a vet vector of 20 integers. The program must generate, from the read vector, another pos vector containing only the positive integer values of vet. From the pos vector, another semdup vector should be generated that contains only one occurrence of each pos value.

pos = []
semdup = []
vet = (input('Entre com 20 números inteiros separados por espaço: ')).split()
if len(vet) != 20:
  print('Quantidade de números digitados diferentes de 20!')
  vet = (input('Entre com os números inteiros separados por espaço: ')).split()
for i in range(20):
  vet[i] = int(vet[i])
print('vet = ',vet)
for i in range(0,20):
  if vet[i] >= 0:
    pos.append(vet[i])
print('pos = ',pos)
semdup = sorted(set(pos))
print('semdup = ',semdup)
  • 1

    Hello, what error appeared when you ran your program? I tested it and it seemed to work right for me

  • Camila, good morning! How Flavio said your code works. It may be that you have some wrong indentation. Hug!

  • It is always good to put the error messages displayed by the compiler. From a read on Stack Overflow Question Checklist.

2 answers

1

In this issue you need to follow a well-defined logic. This logic should follow the following steps:

  1. Implement a vector with 20 elements;
  2. Mount another vector with only positive values of the first vector;
  3. Mount another vector with only positive values and no repetitions of the first vector;
  4. Display the three vectors.

There are several ways you can implement the vector. You can mount the vector with the repetition loop for, while, list compherensio or with the function map.

After you’ve implemented the vector. You need to mount another vector with only positive values and another vector with only positive values and no repetitions. Then display the respective vectors.

One of the ways to implement a code that satisfies this logic is:

vet = list(map(int, input('Digite os valores: ').split()))

pos = list()
semdup = list()
for i in vet:
    if i > 0:
        pos.append(i)
        if i not in semdup:
            semdup.append(i)

print(f'O vetor "vet" é: {vet}')
print(f'O vetor "pos" é: {pos}')
print(f'O vetor "semdup" é: {semdup}')

When we executed this code, we received the following message: Digite os valores: . Right now we must type all 20 values, in the same line, separated for a single space and, press ENTER.

From this moment the code will assemble a list of all the values typed. Later the loop for will traverse the entire vector vet and with the help of 1st block if, will be verified if each value of the respective interaction is greater than 0. If positive, this value will be added to the vector pos. Then the 2nd block if verify that the respective interaction value does not exist in the vector semdup. If this value does not exist in the vector semdup, it will be added to the vector semdup.

After having performed these operations the vectors vet, pos and semdup are displayed.

Observing:

In this code I only used a repeat loop for. With this, we can better greatly optimize the execution of the code.

Let’s test the code

When executing the code, we received the message: Digite os valores: . At this moment we must enter the 20 values, in the same line, separated by a single space and press enter. So we typed...

-4 2 -3 6 6 7 7 -8 9 -10 11 -11 13 13 13 14 -15 18 19 20

...and press enter.

Then the code will perform all operations and provide us with the following output:

O vetor "vet" é: [-4, 2, -3, 6, 6, 7, 7, -8, 9, -10, 11, -11, 13, 13, 13, 14, -15, 18, 19, 20]
O vetor "pos" é: [2, 6, 6, 7, 7, 9, 11, 13, 13, 13, 14, 18, 19, 20]
O vetor "semdup" é: [2, 6, 7, 9, 11, 13, 14, 18, 19, 20]

0

Warning: the following program does not perform checks on the number of entries or perform any validation on them.

The interpreter Python comes with built-in function filter(func, iteravel) that builds an iterator from the elements of an iterable whose function passed in the parameter func returns true:

vet = map(int, input('Digite os valores: ').split())

#A função anônima diz a filter, se o elemento de vet for maior ou igual a zero esse 
#fará parte do resultado. 
pos = list(filter(lambda x: x>=0, vet))

print(pos)

Suggested reading Lambda expressions.

Browser other questions tagged

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