Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6255979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:27:57+00:00 2026-05-24T14:27:57+00:00

Simple question about numpy : I load 100 values to a vector a .

  • 0

Simple question about numpy:

I load 100 values to a vector a. From this vector, I want to create an array A with 2 columns, where one column has name “C1” and second one “C2”, one has type int32 and another int64. An example:

a = range(100)
A = array(a).reshape( len(a)/2, 2)
# A.dtype = ...?

How to define the columns’ types and names, when I create array from a?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:27:58+00:00Added an answer on May 24, 2026 at 2:27 pm

    NumPy structured arrays have named columns:

    import numpy as np
        
    a = range(100)
    A = np.array(list(zip(*[iter(a)] * 2)), dtype=[('C1', 'int32'),('C2', 'int64')])
    print(A.dtype)
    
    [('C1', '<i4'), ('C2', '<i8')]
    

    You can access the columns by name like this:

    print(A['C1'])
    # [ 0  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48
    #  50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98]
    

    Note that using np.array with zip causes NumPy to build an array from a temporary list of tuples. Python lists of tuples use a lot more memory than equivalent NumPy arrays. So if your array is very large you may not want to use zip.

    Instead, given a NumPy array A, you could use ravel() to make A a 1D
    array, and then use view to turn it into a structured array, and then use astype to convert the columns to the desired type:

    a = range(100)
    A = np.array(a).reshape( len(a)//2, 2)
    A = A.ravel().view([('col1','i8'),('col2','i8'),]).astype([('col1','i4'),('col2','i8'),])
    print(A[:5])
    # array([(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)], 
    #       dtype=[('col1', '<i4'), ('col2', '<i8')])
    
    print(A.dtype)
    # dtype([('col1', '<i4'), ('col2', '<i8')])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a language-agnostic question about an algorithm. This comes from a (probably simple)
Simple question, but one that I've been curious about...is there a functional difference between
I want to ask a question about how you would approach a simple object-oriented
This should be a fairly simple question but I know very little about SQL.
This is a simple question about how Python handles data and variables. I've done
I have simple question about .net garbage collection. In the following code I create
i have a simple question about caching, this kind of confuses me lol Im
I've a simple question about objective-c / objective-j syntax. This is a method dataForItemsAtIndexes
A simple question about images. I want to display 5 images across a width
I just have one simple question about XSS attack. I know that you can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.