You should use a list comprehension:
>>> import pprint >>> n = 3 >>> distance = [[[0 for k in xrange(n)] for j in xrange(n)] for i in xrange(n)] >>> pprint.pprint(distance) [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]] >>> distance[0][1] [0, 0, 0] >>> distance[0][1][2] 0
You could have produced a data structure with a statement that looked like the one you tried, but it would have had side effects since the inner lists are copy-by-reference:
>>> distance=[[[0]*n]*n]*n >>> pprint.pprint(distance) [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]] >>> distance[0][0][0] = 1 >>> pprint.pprint(distance) [[[1, 0, 0], [1, 0, 0], [1, 0, 0]], [[1, 0, 0], [1, 0, 0], [1, 0, 0]], [[1, 0, 0], [1, 0, 0], [1, 0, 0]]]
Related Posts:
- How to have an array of arrays in Python
- Understanding NumPy’s einsum
- Using numpy to build an array of all combinations of two arrays
- Convert a 1D array to a 2D array in numpy
- TypeError: list indices must be integers or slices, not str
- ValueError: setting an array element with a sequence
- TypeError: ‘int’ object is not callable
- TypeError: ‘int’ object is not callable
- Python ‘If not’ syntax [duplicate]
- RuntimeWarning: invalid value encountered in divide
- Numpy ValueError: setting an array element with a sequence. This message may appear without the existing of a sequence?
- ValueError: operands could not be broadcast together with shapes (5,) (30,)
- How does numpy.newaxis work and when to use it?
- WinError 2 The system cannot find the file specified (Python)
- sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’)
- Could not find a version that satisfies the requirement tensorflow
- sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’)
- Local variable referenced before assignment?
- ln (Natural Log) in Python
- Convert pandas dataframe to NumPy array
- Python Traceback (most recent call last)
- Unable to plot Double Bar, Bar plot using pyplot for ndarray
- How to pip or easy_install tkinter on Windows
- Cannot find module cv2 when using OpenCV
- How to count the occurrence of certain item in an ndarray?
- size of NumPy array
- What is the meaning of [:] in python [duplicate]
- Why are Python’s ‘private’ methods not actually private?
- How to find list intersection?
- Using Numpy Vectorize on Functions that Return Vectors
- Remove list from list in Python
- Is arr.__len__() the preferred way to get the length of an array in Python?
- How to import files in python using sys.path.append?
- How do I install the yaml package for Python?
- Python: Array v. List
- Change figure size and figure format in matplotlib
- initialize a numpy array
- load csv into 2D matrix with numpy for plotting
- how does \r (carriage return) work in Python
- What is Python buffer type for?
- How to initialize a two-dimensional array in Python?
- How to access the ith column of a NumPy multidimensional array?
- IndexError: index 1 is out of bounds for axis 0 with size 1/ForwardEuler
- Dump a NumPy array into a csv file
- Python: find position of element in array
- numpy with python: convert 3d array to 2d
- TypeError: unsupported operand type(s) for -: ‘list’ and ‘list’
- AttributeError: ‘datetime’ module has no attribute ‘strptime’
- Python add item to the tuple
- How to remove specific elements in a numpy array
- Add single element to array in numpy
- Converting binary to decimal integer output
- How can I read pdf in python?
- No module named setuptools
- Two dimensional array in python
- How to fix: “UnicodeDecodeError: ‘ascii’ codec can’t decode byte”
- How to read a text file into a list or an array with Python
- Python – ‘ascii’ codec can’t decode byte
- Difference between scikit-learn and sklearn
- How to fix “TypeError: len() of unsized object”
- How to detect key presses?
- What is the difference between json.load() and json.loads() functions
- Numpy.dot TypeError: Cannot cast array data from dtype(‘float64’) to dtype(‘S32’) according to the rule ‘safe’
- How to XOR two strings in Python
- TypeError: ‘_io.TextIOWrapper’ object is not subscriptable
- Copy a list of list by value and not reference
- Iterating over a 2 dimensional python list [duplicate]
- Good ways to “expand” a numpy ndarray?
- Split a python list into other “sublists” i.e smaller lists
- scrapy run spider from script
- python error: TypeError: an integer is required
- How to plot an array in python?
- How to create a numpy array of all True or all False?
- How to create a numpy array of lists?
- Removing nan values from an array
- If list index exists, do X
- How to detect key presses?
- Installation of pygame with Anaconda
- Root mean square of a function in python
- urllib and “SSL: CERTIFICATE_VERIFY_FAILED” Error
- Rotating a two-dimensional array in Python
- ‘virtualenv’ is not recognized as an internal or external command, operable program or batch file
- How can I copy a Python string?
- How do I merge lists in python? [duplicate]
- Counting array elements in Python
- What is the meaning of “int(a[::-1])” in Python?
- How to normalize a NumPy array to within a certain range?
- numpy array concatenation error: 0-d arrays can’t be concatenated
- How to completely uninstall python 2.7.13 on Ubuntu 16.04
- Spell Checker for Python
- Sorting arrays in NumPy by column
- How to open html file?
- “Python version 2.7 required, which was not found in the registry” error when attempting to install netCDF4 on Windows 8
- Does python have header files like C/C++?
- Hashing arrays in Python
- In python, how can I print lines that do NOT contain a certain string, rather than print lines which DO contain a certain string:
- input() error – NameError: name ‘…’ is not defined
- Python 2.7 mixing iteration and read methods would lose data
- Homebrew brew doctor warning about /Library/Frameworks/Python.framework, even with brew’s Python installed
- Python: OSError: [Errno 2] No such file or directory: ”