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 5846613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:36:06+00:00 2026-05-22T12:36:06+00:00

I would like to combine several lists or arrays into a single record array.

  • 0

I would like to combine several lists or arrays into a single record array.
In the following code I want to created a record array with two colums: "a" and "b". The first column will contain letters from "a" to "j", the second one will contain numbers from 0 to 9

In [22]: a = list('abcdefghij'); b = range(10); c = numpy.vstack((a, b)).T

In [23]: desc = {'names': ('a', 'b'), 'formats': ('S4', 'f4')}

In [24]: d = numpy.array(c, dtype=desc)

In [25]: d
Out[25]: 
array([[('a', 0.0), ('0', 0.0)],
       [('b', 0.0), ('1', 0.0)],
       [('c', 0.0), ('2', 0.0)],
       [('d', 0.0), ('3', 0.0)],
       [('e', 0.0), ('4', 0.0)],
       [('f', 0.0), ('5', 0.0)],
       [('g', 0.0), ('6', 0.0)],
       [('h', 0.0), ('7', 0.0)],
       [('i', 0.0), ('8', 0.0)],
       [('j', 0.0), ('9', 0.0)]], 
      dtype=[('a', '|S4'), ('b', '<f4')])

In [26]: d['a']
Out[26]: 
array([['a', '0'],
       ['b', '1'],
       ['c', '2'],
       ['d', '3'],
       ['e', '4'],
       ['f', '5'],
       ['g', '6'],
       ['h', '7'],
       ['i', '8'],
       ['j', '9']], 
      dtype='|S4')

In [27]: d['b']
Out[27]: 
array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]], dtype=float32)

The result is completely not what I would expect. What I want is:

In [XX]: d['a']
Out[XX]: array(['a', 'b', 'c', ..., 'j'])
In [XX]: d['b']
Out[XX]: array([1., 2., 3., ..., 9.])

EDIT

My goal was to be able to create record arrays from already existing numpy.array, and not only from the individual lists. Following the answer by Sven Marnach, I had several tries and errors and this is what I got:

d = array(map(lambda l: tuple(l[0]), zip(c)), dtype=desc)

This seems to be a pretty ugly solution. Does anyone has a better one?

  • 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-22T12:36:07+00:00Added an answer on May 22, 2026 at 12:36 pm

    Following from your comment on Sven’s answer, if you don’t have a and b, then do the following and populate c later,

    from numpy import recarray
    
    desc = {'names': ('a', 'b'), 'formats': ('S4', 'f4')}
    c = recarray((10,),dtype=desc)
    
    a = list('abcdefghij'); b = range(10)
    
    c['a'] = a
    c['b'] = b
    
    print r
    

    gives,

    [('a', 0.0) ('b', 1.0) ('c', 2.0) ('d', 3.0) ('e', 4.0) ('f', 5.0)
     ('g', 6.0) ('h', 7.0) ('i', 8.0) ('j', 9.0)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several tables in my ERD which which I would like to combine
I would like to combine 2 commands into 1, if its possible. <%= render
I would like a fairly efficient way to condense an entire table to a
While revising some old c++ code, I ran across several bitflags defined as enums.
I have several sets of similar objects (labels, progress bars) on a form in
I have several measures: Profit and loss (PNL). Win to loss ratio (W2L). Avg
I want to create a window that will be the main window and that
I'm trying to create a small 2D game in Javascript/Canvas which consists of several
I'd like suggestions for the design of a CRUD business app using Silverlight 4,
I have a table called People with a columns of datatype xml called properties

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.