. Advertisement .
..3..
. Advertisement .
..4..
This tutorial will show you how to print matrix in Python. Its examples involve two-dimensional matrices, but you can use them to deal with other shapes as well.
Print Matrix In Python
With print()
Since Python doesn’t have a built-in data type for matrixes, we will use NumPy arrays to represent them instead. The easiest way to print them to the output is to use the print()
function with the array you want to print as the argument.
Example:
>>> a = np.array([[1,2,3],[3,4,5],[7,8,9]])
>>> print(a)
[[1 2 3]
[3 4 5]
[7 8 9]]
This solution doesn’t require any special method or function. As you may already know, the print()
function is available in Python 3. It prints objects to a text stream.
Python will convert non-keyword arguments like NumPy arrays to strings in the same way the str()
function works. This string will then be written to the stream.
This simple solution can be enough for many situations. But if you want to prettify the output, there are other options as well.
With for Loops
You can make a loop that goes through every element in the NumPy array and prints its value with a custom format to the output.
The following example is an implementation of this idea:
import numpy as np
a = np.array([[1,2,3],[3,4,5],[7,8,9]])
for row in a:
print (' '.join(map(str, row)))
Output:
1 2 3
3 4 5
7 8 9
We will explain why you will need the map()
and join()
functions. When the for loop goes through the array above, it doesn’t visit each element. NumPy arrays will return each row the iterator invokes, which means the for loop above runs three times over the array.
You can check this by calling an index from the array:
>>> a[0]
array([1, 2, 3])
We use the map()
function to apply the str()
function to every item in this row and return a map object that contains the results. The str()
function has helped us convert every element to a string, which will be stored in the map object.
>>> map(str, a[0])
<map object at 0x7f7da0bf19f0>
Still, we can’t print the output yet. This is when the join()
method comes into play. Its syntax:
string.join(iterable)
This method takes an iterable, such as the map object above, as the parameter. It will then return a string that has all the elements of that iterable, separated by the string.
The join()
method is a convenient solution for creating strings from iterable objects. When given the map object, it will use the ‘ ‘ string to separate the elements for us:
>>> ' '.join(map(str, a[0]))
'1 2 3'
The for loop repeats this step with the remaining rows, and we have a more human-readable output of the original matrix.
With List Comprehension
In Python and other programming languages, list comprehensions allow you to create a list based on existing lists. On many fronts, it works like a for loop. But list comprehensions are a more elemental solution that can result in fewer lines of code.
For instance, this is how you can rewrite the previous examples with a list comprehension:
a = np.array([[1,2,3],[3,4,5],[7,8,9]])
print('\n'.join([''.join(['{:4}'.format(element) for element in row])
for row in a]))
Output:
1 2 3
3 4 5
7 8 9
This list comprehension goes through every row, in which it prints all the elements. When the row ends, it will print the newline character \n
to prepare the printing of the next row.
Note: you can also learn how to use list comprehensions to create a list of lists with this guide.
Summary
You can print matrix in Python with the print()
function, a for loop, or a list comprehension. The final two solutions are more complicated, but they give you more control over how the output would look like.
Leave a comment