Python Forum
Turn list of arrays into an array of lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turn list of arrays into an array of lists
#7
It is quite complicated to reproduce this in a simplified way but I will give it a try. I hope it will be clear.

First of all I have an array of the form:

first_array=np.array([0, 0, 0, 0, 0, 1, 2, 2, 2, 3, 3, 4], dtype=int64)
Then I have a function_a that takes this array and calculates a value for each entry which is summed up in a 1-dimensional array with the length of 5. It is hard to find an easy example for this calculation without posting a ton of code, so this is replaced here by a simple random_function, that returns an array of dtype=64 (not modifiable unfortunately):

def function_a(first_array):
    
    total_results = [0 for i in range(5)]

    for row_index in range(first_array.shape[0]):

        total_results += random_function([row_index])
    
    return total_results



def random_function(entry_array):
        
    result_for_entry = np.random.choice([0, 1], size=5, p=[.1, .9])
    result_for_entry = result_for_entry.astype(np.int64)
    
    return result_for_entry


function_a(first_array)
Output:
array([ 9, 11, 10, 12, 11], dtype=int64)
But I have to to this for a larger data set of this example form (here only 5 arrays are assumed, in fact there are 15.000):

testdata=np.array([array([0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 4, 4], dtype=int64),
       array([0, 1, 2, 2, 2, 3, 3, 4, 4], dtype=int64),
       array([0, 0, 1, 3, 4], dtype=int64), array([0, 0], dtype=int64),
       array([0, 1, 2, 2, 4, 4], dtype=int64)], dtype=object)
For this purpose I wrote function_b which results in the mentioned result structure of a list of arrays:

def function_b(testdata):

    results = [[0 for i in range(5)] for k in range(5)]
    
    for i in range(5):
        results[i] = function_a(testdata[i])
       
        
    return results

function_b(testdata)
Output:
[array([12, 10, 11, 12, 11], dtype=int64), array([8, 7, 9, 8, 7], dtype=int64), array([5, 5, 5, 5, 5], dtype=int64), array([2, 2, 2, 2, 2], dtype=int64), array([6, 6, 6, 6, 5], dtype=int64)]
I hope I explained the structure in an understandable way. Help is much appreciated how I can get the final results as an array of lists.

Thank you!
Reply


Messages In This Thread
RE: Turn list of arrays into an array of lists - by Cola_Reb - Jul-20-2022, 06:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  comparing floating point arrays to arrays of integers in Numpy amjass12 0 1,651 Jul-26-2021, 11:58 AM
Last Post: amjass12
  Array lists vs Linked Lists sabe 2 1,805 Nov-28-2020, 12:31 AM
Last Post: perfringo
  Numpy arrays and compatability with Fortran arrays merrittr 0 1,909 Sep-03-2019, 03:54 AM
Last Post: merrittr
  Convert element of list to integer(Two dimentional array) zorro_phu 3 4,838 Jun-12-2018, 04:49 AM
Last Post: zorro_phu
  Importing matlab cell array (.mat) into a python list scanato 0 8,695 Nov-15-2017, 11:04 AM
Last Post: scanato
  Calculate the mean of an array across dimension with lists of different length rakhmadiev 2 4,575 Aug-01-2017, 02:52 AM
Last Post: Larz60+
  Are lists Python's name for "variable array" Luke_Drillbrain 6 5,006 Apr-21-2017, 09:14 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020