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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:18:26+00:00 2026-06-03T16:18:26+00:00

In NumPy, whole segments of arrays can be assigned using : as a wildcard

  • 0

In NumPy, whole segments of arrays can be assigned using : as a wildcard for index ranges. For example:

>>> (n, m) = (5,5)
>>> a = numpy.array([[0 for i in range(m)] for j in range(n)])
>>> a
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]])

>>> for i in range(n):
...     a[i, :] = [1 for j in range(m)]
>>> a
array([[1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]])

However, numpy.array only holds numeric data. I need an array type which can hold arbitrary objects and can be addressed like NumPy arrays. What should I use?

EDIT: I’d like the full flexibility of this range assignment syntax, e.g. this should work, too:

>>> a[:,1] = 42
>>> a
array([[ 1, 42,  1,  1,  1],
       [ 1, 42,  1,  1,  1],
       [ 1, 42,  1,  1,  1],
       [ 1, 42,  1,  1,  1],
       [ 1, 42,  1,  1,  1]])
  • 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-06-03T16:18:28+00:00Added an answer on June 3, 2026 at 4:18 pm

    Maybe I’m missing something here but numpy does in fact hold objects as well as numbers.

    In [1]: import numpy
    
    In [2]: complex = {'field' : 'attribute'}
    
    In [3]: class ReallyComplex(dict):
       ...:     pass
       ...: 
    
    In [4]: a = numpy.array([complex,ReallyComplex(),0,'this is a string'])
    
    In [5]: a
    Out[5]: array([{'field': 'attribute'}, {}, 0, this is a string], dtype=object)
    In [6]: subsection = a[2:]
    
    In [7]: subsection
    Out[7]: array([0, this is a string], dtype=object)
    

    When you place complex objects into a numpy array the dtype becomes object. You can access members and slices of the array as you would with normal numpy arrays. I’m not familiar with the serialization but you may experience drawbacks in that area.

    If you are convinced that numpys are not the way to go a standard Python list is a great way to maintain a collection of objects and you may also slice the python list the very similar to the numpy array.

     std_list = ['this is a string', 0, {'field' : 'attribute'}]
     std_list[2:]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to index large 3D arrays using a 2D array of indicies from
I am graphing data from a numpy array using matplotlib imshow. However, some points
import numpy a = numpy.array([20090913, 20101020, 20110125]) Can you explain why numpy.datetime64(a.astype(S8).tolist()) converts correctly
When I load an array using numpy.loadtxt, it seems to take too much memory.
After using it for a while, I really like the Numpy multi-dimensional array. It's
How can I build a numpy array out of a generator object? Let me
I have an numpy array which I save to a image using savefig(). Then
I'm using numpy to create a cube array with sides of length 100, thus
The numpy.equal function does not work if a list or array contains strings: >>>
I'm creating a numpy array which is to be filled with objects of a

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.