Integer array in Python

How can I define an array of integers in Python code

Tell me if this code is normal. or not

pos = [int]

len = 99

for i in range (0,99):
    pos[i]=7
+3
source share
6 answers

Why not just:

pos = [7] * 99

This, in my opinion, is the most pythonic.

+11
source
import array

pos = array.array('l', 7 * [99])

The Python module's standard module's array module is the only way to make an array that comes with Python (a third-party module numpyoffers other ways, but they need to be downloaded and installed separately) - what your Q does, and every answer still builds list, not a array.

, , pos, Q, As , - (32- , ), , ( , , , ).

, array, list ( list , ), - , , , , ! -)

+5

python, no pos=[int] , :

pos=[]
for i in range(99):
    pos.append(7)
+4

pos = [7] * 99
print pos #will print the whole array [7, 7, .... 7]
+2

, , python, :

pos = []

99 7:

pos = [7] * 99

If you want to populate an array based on a template:

pos = [i for i in range(99)]
+1
source

One of the methods:

pos = [7 for _ in xrange(0,99)]

in Python 2 or:

pos = [7 for _ in range(0,99)]

in Python 3. This is a list of concepts and is easy to extend for more complex work.

also:

pos = [int]

doesn't make much sense. You create a list whose only type is int.

0
source

Source: https://habr.com/ru/post/1746946/


All Articles