NameError: name 'array' is not defined in python

I get NameError: name 'array' is not definedin python error when I want to create an array, for example:

a = array([1,8,3])

What am I doing wrong? How to use arrays?

+5
source share
4 answers

If you need a container to hold a bunch of things, then lists might be the best choice:

a = [1,8,3]

A type

dir([])

from the Python interpreter to see methods that contain support, such as append, pop, reverse, and sort. Lists also support lists and the P itonable interface:

for x in a:
    print x

y = [x ** 2 for x in a]
+2
source

You need to import the method arrayfrom the module.

from array import array

http://docs.python.org/library/array.html

+42
source

Python list ( ).

NumPy NumPy:

import numpy as np

a = np.array([1,8,3])

, NumPy, , , list.

+12

You probably don't need an array. Try using a list:

a = [1,8,3]

Python lists execute as dynamic arrays in many other languages.

+2
source

All Articles