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
#1
Hey everyone,

I am currently struggling with the structure of data I receive. As a result of another (not important here) function I get a list filled with numpy arrays at a large scale (about 20 thousand entries):


first_array=[array([[1, 0, 2, 4, 3]], dtype=int64),
 array([[2, 6, 8, 3, 3]], dtype=int64),
 array([[0, 0, 4, 3, 3]], dtype=int64),
 array([[4, 4, 3, 2, 3]], dtype=int64),
...]
For the further procession of this data I need this input in the reverse structure: An array of lists like the following form:

output_array=array([
       [1, 0, 2, 4, 3],
       [2, 6, 8, 3, 3],
       [0, 0, 4, 3, 3],
       [4, 4, 3, 2, 3],
....])
Is there a way of doing this in a way that keeps the computational time low? Huh
Reply
#2
If you change "array" to "np.array" and int64 to "int", then at least the code works:

import numpy as np

Input=[np.array([[1, 0, 2, 4, 3]], dtype=int),
       np.array([[2, 6, 8, 3, 3]], dtype=int),
       np.array([[0, 0, 4, 3, 3]], dtype=int),
       np.array([[4, 4, 3, 2, 3]], dtype=int)]

Input2Array = np.asarray(Input).reshape(4, 5)

print(Input2Array)
Output:
[[1 0 2 4 3] [2 6 8 3 3] [0 0 4 3 3] [4 4 3 2 3]]
Cola_Reb likes this post
Reply
#3
Just a comment - input is a terrible name for a variable, as it overrides the standard function. You can't use input() after that...
Cola_Reb likes this post
Reply
#4
(Jul-19-2022, 05:21 PM)paul18fr Wrote: If you change "array" to "np.array" and int64 to "int", then at least the code works:

import numpy as np

first_array=[np.array([[1, 0, 2, 4, 3]], dtype=int),
       np.array([[2, 6, 8, 3, 3]], dtype=int),
       np.array([[0, 0, 4, 3, 3]], dtype=int),
       np.array([[4, 4, 3, 2, 3]], dtype=int)]

Input2Array = np.asarray(Input).reshape(4, 5)

print(Input2Array)
Output:
[[1 0 2 4 3] [2 6 8 3 3] [0 0 4 3 3] [4 4 3 2 3]]

Is there also a possibility if it is not possible to change the numpy array to int-type?
Reply
#5
Please pay attention to post by jeffsummers. It is important, taboo actually to use reserved words in your code.
input is used by python, and you are redefining it.
Cola_Reb likes this post
Reply
#6
I fully agree with jefsummers and Larz60+, and that's why I've changed in first letter in upper case ... but it may lead to a misunderstanding in a future!

@Cola_Reb: it might be interesting to show how did you make your list
Cola_Reb likes this post
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  comparing floating point arrays to arrays of integers in Numpy amjass12 0 1,637 Jul-26-2021, 11:58 AM
Last Post: amjass12
  Array lists vs Linked Lists sabe 2 1,775 Nov-28-2020, 12:31 AM
Last Post: perfringo
  Numpy arrays and compatability with Fortran arrays merrittr 0 1,890 Sep-03-2019, 03:54 AM
Last Post: merrittr
  Convert element of list to integer(Two dimentional array) zorro_phu 3 4,776 Jun-12-2018, 04:49 AM
Last Post: zorro_phu
  Importing matlab cell array (.mat) into a python list scanato 0 8,675 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,534 Aug-01-2017, 02:52 AM
Last Post: Larz60+
  Are lists Python's name for "variable array" Luke_Drillbrain 6 4,955 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