Don’t use [[v]*n]*n
, it is a trap!
>>> a = [[0]*3]*3
>>> a
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> a[0][0]=1
>>> a
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]
but
t = [ [0]*3 for i in range(3)]
works great.
More Related Contents:
- Matrix Transpose in Python [duplicate]
- Understanding NumPy’s einsum
- Intuition and idea behind reshaping 4D array to 2D array in NumPy
- What is the difference between flatten and ravel functions in numpy?
- How to define a two-dimensional array?
- How do I make a flat list out of a list of lists?
- Why is numpy’s einsum faster than numpy’s built in functions?
- Calculate mean across dimension in a 2D array
- How to count the occurrence of certain item in an ndarray?
- How do I count the occurrence of a certain item in an ndarray?
- Selecting specific rows and columns from NumPy array
- Swap slices of Numpy arrays
- How do I use np.newaxis?
- Vectorized NumPy linspace for multiple start and stop values
- Sort multidimensional array based on 2nd element of the subarray
- Check if two 3D numpy arrays contain overlapping 2D arrays
- Selecting Random Windows from Multidimensional Numpy Array Rows
- How to split a numpy array in fixed size chunks with and without overlap?
- How to find the index of a value in 2d array in Python?
- remove zero lines 2-D numpy array
- Animation with pcolormesh routine in matplotlib, how do I initialize the data?
- What is the purpose of meshgrid in Python / NumPy?
- numpy reverse multidimensional array
- Iterating over a 2 dimensional python list [duplicate]
- Good ways to “expand” a numpy ndarray?
- python tilde unary operator as negation numpy bool array
- How to sort a 2D list? [closed]
- Access n-th dimension in python [duplicate]
- Converting List Comprehensions to For Loops in Python
- Indexing numpy array with another numpy array