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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:26:14+00:00 2026-06-07T06:26:14+00:00

I have a dictionary that is keyed by date and filled with classes that

  • 0

I have a dictionary that is keyed by date and filled with classes that have an attribute that is a numpy.array. I want to use np.dstack to make one large array from all the arrays in the dictionary. My current code is like this:

import numpy as np
#PARTS is my dictionary
#the .partposit is the attribute that is an array of shape (50000, 12)
ks = sorted(PARTS.keys())
p1 = PARTS[ks[0]].partposit
for k in ks[1:]:
    p1 = np.dstack((p1, PARTS[k].partposit))

My result is as I expect:

In [67]: p1.shape
Out[67]: (50000, 12, 163)

However, it is quite slow. Is there a more efficient way to do this?

  • 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-07T06:26:16+00:00Added an answer on June 7, 2026 at 6:26 am

    you could try this:

    >>> import numpy as np
    >>> class A:
    ...     def __init__(self, values):
    ...         self.partposit = values
    ... 
    >>> PARTS = dict((index, A(np.zeros((50000, 12)))) for index in xrange(163))
    >>> p1 = np.dstack((PARTS[k].partposit for k in sorted(PARTS.keys())))
    >>> p1.shape
    (50000, 12, 163)
    >>> 
    

    it took a few seconds to stack it on my machine.

    >>> import timeit
    >>> timeit.Timer('p1 = np.dstack((PARTS[k].partposit for k in sorted(PARTS.keys())))', "from __main__ import np, PARTS").timeit(number = 1)
    2.1245520114898682
    

    numpy.dstack takes in a sequence of arrays and stacks them together as such it would be much faster if we just give it the list instead of continuously stacking them ourselves.

    numpy.dstack(tup)

    Stack arrays in sequence depth wise (along third axis).
    Takes a sequence of arrays and stack them along the third axis to make a single array.

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.dstack.html

    I was also curious as to see how long your method would be:

    >>> import timeit
    >>> setup = """
    ... import numpy as np
    ... #PARTS is my dictionary
    ... #the .partposit is the attribute that is an array of shape (50000, 12)
    ... 
    ... class A:
    ...     def __init__(self, values):
    ...         self.partposit = values
    ... 
    ... PARTS = dict((index, A(np.zeros((50000, 12)))) for index in xrange(163))
    ... ks = sorted(PARTS.keys())
    ... """
    >>> stack = """
    ... p1 = PARTS[ks[0]].partposit
    ... for k in ks[1:]:
    ...     p1 = np.dstack((p1, PARTS[k].partposit))
    ... """
    >>> timeit.Timer(stack, setup).timeit(number = 1)
    67.69684886932373
    

    ouch!

    >>> numpy.__version__
    '1.6.1'
    
    $ python --version
    Python 2.6.1
    

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary of strings that i want the user to be able
I have a dictionary where I want to alphabetically sort the keys that have
I want to have Dictionary that would be 'Observable' in order to throw events
I want to have a dictionary that assigns a value to a set of
i have one dictionary that has entries that looks like this: dictionary[ABC.123] = Test
I want to have a Dictionary that maps strings to generic lists of varying
I have a dictionary that I read from a plist. I want to create
I have a dictionary that has keys associated with lists. mydict = {'fruits': ['banana',
I have a dictionary that hosts diameter(in mm) and worth of euro coins: CoinsDiameters
Is it possible to clear a Flex flash.utils.Dictionary ? I have a Dictionary that

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.