Vectors in Python

Asked

Viewed 69,151 times

7

Okay, I’m coming from line C, and I’m having a hard time creating vectors in python. In C if I write vet = [5]; I would create a vector called vet with 5 indices. I wish I knew how to do this in python but I’m a beginner and I’m not getting it. The point is, how I determine the amount of indices my vector will have?

3 answers

8

The shortest answer is: it doesn’t define. Python is a high-level programming language and dynamic typing, you do not need to limit the size of your vector.

In fact, the type in Python that most resembles the vector of C is the list. For you to set a new list, just do:

>>> meuVetor = []

You can confirm the type of the variable by doing:

>>> print(type(meuVetor))
<class 'list'>

At this point it is interesting to remember that all in Python is object, so same as type list is native, it is a class. You can check all vector methods using the function dir:

>>> print(dir(meuVetor))
[..., 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

You can enter new values in the vector by the method append:

>>> meuVetor.append(1)
>>> print(meuVetor)
[1]
>>> meuVetor.append(2)
>>> print(meuVetor)
[1, 2]

Theoretically you can do this indefinitely. The size of the vector will vary depending on its use. It grows and decreases by demand.

To access the elements, it is very similar to C:

>>> print(meuVetor[0])
1
>>> print(meuVetor[1])
2

You can initialize the vector with a predefined number of elements:

>>> meuVetor = [0]*5
>>> print(meuVetor)
[0, 0, 0, 0, 0]

But this is usually unnecessary in more basic applications.

To go through all elements of the vector, just use the for:

>>> for(valor in meuVetor):
...     print(valor)
0
0
0
0
0

This will work regardless of vector size.

If you need to at some point check what is the size of your vector, you can use the function len:

>>> print(len(meuVetor))
5
>>> print(len([0] * 10))
10

For future references, while reading the documentation, keep in mind that type list is mutable.

  • Got it, thanks a lot for your help Anderson

2

Python works with lists:

lista = [1, 2, 3]

Which are variants of size and of guy, independently. Unlike in the case of Array of some other languages.

If you want to create a list with a predefined number of elements, in addition to the assignment, you can do:

lista = [None]*n

Where n is the number of elements. None is the empty object.

In python2, you can also use range(), creating a numerical sequence in the form of a list. However, in python3, this same range() is only an eternal object usually covered by a for, not a list.

  • Thanks for your help Samir

-2

I usually do this

vetor=list(range(5))
for i in range(0,6):
   vetor[i] = 1

you can creat a vector of five positions

  • Welcome to Stack Overflow in Portuguese the Lusophone version of Stack Overflow. This page is exclusive to Portuguese-speaking users. Here all communication should be done in Portuguese. If you want to use English as a language I suggest you visit the Stack Overflow in English

Browser other questions tagged

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