GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — NumPy: Create an array of zeros and three column types - w3resource

NumPy: Create an array of zeros and three column types

Last update on August 29 2025 12:45:30 (UTC/GMT +8 hours)


Zeros Array with Multiple Column Types

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

Sample Solution:

Python Code:

# Importing the NumPy library and aliasing it as 'np'
import numpy as np

# Creating a NumPy array 'x' filled with zeros, with a structured data type
# The dtype specifies a structure containing an integer, a floating-point number, and a string of specific lengths
x = np.zeros((3,), dtype=('i4,f4,a40'))

# Creating a list 'new_data' containing tuples with structured data: integer, float, and string
new_data = [(1, 2., "Albert Einstein"), (2, 2., "Edmond Halley"), (3, 3., "Gertrude B. Elion")]

# Assigning the 'new_data' list to the array 'x', filling the array with the provided structured data
x[:] = new_data

# Printing the resulting array 'x' with structured data
print(x) 

Sample Output:

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

Explanation:

In the above code:

‘np.zeros((3,), dtype=('i4,f4,a40'))’ creates a structured 1D NumPy array x with 3 elements. Each element has a composite data type consisting of three fields: an int32 ('i4'), a float32 ('f4'), and a fixed-length string with 40 characters ('a40').

new_data = [(1, 2., "Albert Einstein"), (2, 2., "Edmond Halley"), (3, 3., "Gertrude B. Elion")]: This code defines a list of tuples named new_data, which contains three tuples. Each tuple has three elements, corresponding to the structure of the array x.

x[:] = new_data: This line assigns the values from new_data to the structured array x. The slice [:] ensures that all elements of x are replaced with the corresponding elements from new_data.

print(x): Finally printf() function prints the resulting structured array ‘x’.


For more Practice: Solve these Related Problems:

  • Write a NumPy program to create a structured array with integer, float, and string fields initialized to default values.
  • Construct a record array with multiple column types and verify field names and types using np.dtype.
  • Create a zero-filled structured array with custom field names, then populate it with sample data.
  • Implement a function that initializes a multi-type array and updates one field while preserving others.

Go to:

  • NumPy Array Exercises Home ↩
  • NumPy Exercises Home ↩

PREV : Combine 1D and 2D Arrays
NEXT : Apply Cube Function to All Elements


Python-Numpy Code Editor:

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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.