w3resource

NumPy: Create an array of zeros and three column types

NumPy: Array Object Exercise-75 with Solution

Write a NumPy program to create an array of zeros and three column types (integer, float, character).

Sample Solution:

Python Code:

import numpy as np
x = np.zeros((3,), dtype=('i4,f4,a40'))
new_data = [(1, 2., "Albert Einstein"), (2, 2., "Edmond Halley"), (3, 3., "Gertrude B. Elion")]
x[:] = new_data
print(x)

Sample Output:

[(1,  2., b'Albert Einstein') (2,  2., b'Edmond Halley')               
 (3,  3., b'Gertrude B. Elion')]

Python Code Editor:

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

Previous: Write a NumPy program to combine a one and a two dimensional array together and display their elements.
Next: Write a NumPy program to create a function cube which cubes all the elements of an array.

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}