w3resource

NumPy: Save as text a matrix which has in each row 2 float and 1 string at the end

NumPy: Array Object Exercise-164 with Solution

Write a NumPy program to save as text a matrix which has in each row 2 float and 1 string at the end.

Sample Solution:

Python Code:

import numpy as np
matrix = [[1, 0, 'aaa'], [0, 1, 'bbb'], [0, 1, 'ccc']]
np.savetxt('test', matrix, delimiter='  ', header='string', comments='', fmt='%s')

Sample Output:

string
1  0  aaa
0  1  bbb
0  1  ccc

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Create two arrays of six elements. Write a NumPy program to count the number of instances of a value occurring in one aray on the condition of another array.
Next: Write a NumPy program to merge three given NumPy arrays of same shape.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}