I get NameError: name 'array' is not defined
in python error when I want to create array, for example:
a = array([1,8,3])
What am I doing wrong? How to use arrays?
I get NameError: name 'array' is not defined
in python error when I want to create array, for example:
a = array([1,8,3])
What am I doing wrong? How to use arrays?
If you need a container to hold a bunch of things, then lists might be your best bet:
a = [1,8,3]
Type
dir([])
from a Python interpreter to see the methods that lists support, such as append, pop, reverse, and sort. Lists also support list comprehensions and Python's iterable interface:
for x in a: print x y = [x ** 2 for x in a]
You need to import the array
method from the module.
from array import array
For basic Python, you should just use a list
(as others have already noted).
If you are trying to use NumPy and you want a NumPy array:
import numpy as np a = np.array([1,8,3])
If you don't know what NumPy is, you probably just want the list
.
You probably don't want an array. Try using a list:
a = [1,8,3]
Python lists perform like dynamic arrays in many other languages.