You’re technically trying to index an uninitialized array. You have to first initialize the outer list with lists before adding items; Python calls this “list comprehension”.
# Creates a list containing 5 lists, each of 8 items, all set to 0 w, h = 8, 5 Matrix = [[0 for x in range(w)] for y in range(h)]
#You can now add items to the list:
Matrix[0][0] = 1 Matrix[6][0] = 3 # error! range... Matrix[0][6] = 3 # valid
Note that the matrix is “y” address major, in other words, the “y index” comes before the “x index”.
print Matrix[0][0] # prints 1 x, y = 0, 6 print Matrix[x][y] # prints 3; be careful with indexing!
Although you can name them as you wish, I look at it this way to avoid some confusion that could arise with the indexing, if you use “x” for both the inner and outer lists, and want a non-square Matrix.
Related Posts:
- ‘Syntax Error: invalid syntax’ for no apparent reason
- Transpose/Unzip Function (inverse of zip)?
- How to correct TypeError: Unicode-objects must be encoded before hashing?
- numpy matrix vector multiplication
- data type not understood
- How can I plot a confusion matrix? [duplicate]
- Singular matrix issue with Numpy
- SyntaxError: multiple statements found while compiling a single statement
- SyntaxError: multiple statements found while compiling a single statement
- Python 3.6.0 syntax error “Missing parentheses in call to ‘print’ [duplicate]
- Python: SyntaxError: keyword can’t be an expression
- How to get element-wise matrix multiplication (Hadamard product) in numpy?
- SyntaxError: cannot assign to operator
- (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
- Inverse of a matrix using numpy
- Checking whether pip is installed?
- Don’t understand this SyntaxError: illegal target for annotation
- (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
- Python: cannot concatenate ‘str’ and ‘int’ objects error
- Python 3: Multiply a vector by a matrix without NumPy
- What are the differences between numpy arrays and matrices? Which one should I use?
- Convert a 1D array to a 2D array in numpy
- Conditional indexing with Numpy ndarray
- How can I reverse a list in Python?
- How to use filter, map, and reduce in Python 3
- What does enumerate() mean?
- Does Python have a ternary conditional operator?
- What is the difference between Python’s list methods append and extend?
- Iterating over dictionaries using ‘for’ loops
- What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
- How to reset index in a pandas dataframe? [duplicate]
- Python: Find in list
- pandas read_json: “If using all scalar values, you must pass an index”
- Web scraping redoc web api
- syntaxerror: “unexpected character after line continuation character in python” math
- ValueError : I/O operation on closed file
- How to set environment variables in Python?
- I keep getting this error for my simple python program: “TypeError: ‘float’ object cannot be interpreted as an integer”
- Numpy std calculation: TypeError: cannot perform reduce with flexible type
- Regex: AttributeError: ‘NoneType’ object has no attribute ‘groups’
- Internal Redirect in Flask
- ValueError: shape mismatch: objects cannot be broadcast to a single shape
- ImportError: No module named ‘Tkinter’
- syntax error when using command line in python
- ImportError: numpy.core.multiarray failed to import
- Matplotlib automatic legend outside plot
- Syntax behind sorted(key=lambda: …)
- convert nan value to zero
- How to install pip3 on Windows?
- Is there a NumPy function to return the first index of something in an array?
- Tkinter scrollbar for frame
- Python Setup Disabling Path Length Limit Pros and Cons?
- How to add a string in a certain position?
- This can be done without regex:
- The difference between ‘+=’ and ‘=+’?
- How to fix “Attempted relative import in non-package” even with __init__.py
- Sorting a set of values
- Git Bash won’t run my python files?
- Can only use .dt accessor with datetimelike values
- Convert XML to CSV file
- How to install xgboost in Anaconda Python (Windows platform)?
- 3-dimensional array in numpy
- OpenCV NoneType object has no attribute shape
- How to use an image for the background in tkinter?
- Python locale error: unsupported locale setting
- sklearn Logistic Regression “ValueError: Found array with dim 3. Estimator expected <= 2."
- Sqlite3, OperationalError: unable to open database file
- What does ‘index 0 is out of bounds for axis 0 with size 0’ mean?
- How to return dictionary keys as a list in Python?
- Python Error – int object has no attribute
- How to save a dictionary to a file?
- Python List object attribute ‘append’ is read-only
- Infinite for loops possible in Python?
- RuntimeError: module compiled against API version a but this version of numpy is 9
- Python3 Error: TypeError: Can’t convert ‘bytes’ object to str implicitly
- How to run Pip commands from CMD
- Sorting and Grouping Nested Lists in Python
- No module named ‘tqdm’
- Compute a confidence interval from sample data
- Can’t get Python to import from a different folder
- How to crop an image in OpenCV using Python
- convert csv file to list of dictionaries
- What’s the difference between “virtualenv” and “-m venv” in creating Virtual environments(Python)
- OperationalError: database is locked
- Decode Hex String in Python 3
- Python ImportError: No module named wx
- print(__doc__) in Python 3 script
- How to print a linebreak in a python function?
- Pandas dataframe groupby plot
- Download Returned Zip file from URL
- Python 3 TypeError: must be str, not bytes with sys.stdout.write()
- tkinter gui layout using frames and grid
- inverting image in Python with OpenCV
- Meaning of list[-1] in Python
- ValueError: Cannot set a frame with no defined index and a value that cannot be converted to a Series
- SMTP AUTH extension not supported by server
- How to install PyQt4 on Windows using pip?
- How to install PyQt4 on Windows using pip?
- python dictionary error AttributeError: ‘list’ object has no attribute ‘keys’
- What is a mixin, and why are they useful?