简体   繁体   中英

How to combine list of numpy arrays like this?

I have a list of multiple numpy arrays. Each array is 1D horizontal and their vertical length varies, and in each column is a uint16. An example of two short arrays that could be in my list:

[array([[134],
       [133],
       [ 46],
       [133]], dtype=uint16)]...
array([[ 18],
       [ 45],
       [121],
       [ 45],
       [ 18],
       [ 17], dtype=uint16)]...

As I mentioned, the vertical lengths vary as well as the integers inside the arrays as well as the amount. The reason I have multiple arrays like this in a list is because each integer in each array represents an index of an element in a separate array. Each array (the one with indices, as shown above) has another array associated to it. Instead of multiple arrays, I want to stack everything into one array. I successfully did this for the separate arrays (not the ones shown). However, I'm unable to stack the array with the corresponding indices because the index of each element changed. For example, in the second array shown above, if 134 was the largest integer in the first array, then 18 in the second array should become 152 because that's the index of the element in represents in the new stacked array of elements. I've been stuck on this for a while, hopefully I'm being detailed enough. I'm unsure how to even go about this problem. Is there a quick way to do something like this? How would you do so? The two arrays above are just quick examples, obviously it doesn't make since to have an array of indices like that (eg there should be all 0 - 134 in there at least somewhere). I should also mention that an index can be repeated in the array. I think what I'm wanting is to take the highest number of the previous array and add them all to the array after it. Update: Minimum reproducible code with input/desired output:

import numpy as np

arr1 = np.array([0, 1, 2, 2, 2, 1, 0, 0, 2], dtype=">i").reshape(9, 1)
arr2 = np.array([0, 2, 2, 2, 0, 2, 0, 1, 1], dtype=">i").reshape(9, 1)
list_of_arrs = [arr1, arr2]
# using list_of_arrs, i want the desired output:
# -> [[0] [1] [2] [2] [2] [1] [0] [0] [2] [3] [5] [5] [5] [3] [5] [3] [4] [4]]

Note: Even though the arrays contain the same range of integers 0 - 2 and the same lengths, the actual arrays may or may not. Basically I want to take the highest integer of the previous array and take that + 1 and add that to every integer of the next array, and make sure it uses the new array for doing the next one and so on...

You should use np.append instead of simple concatenation in this case.

import numpy as np

arr1 = np.array([0, 1, 2, 2, 2, 1, 0, 0, 2], dtype=">i").reshape(9, 1)
arr2 = np.array([0, 2, 2, 2, 0, 2, 0, 1, 1], dtype=">i").reshape(9, 1)

list_of_arrs = np.append(arr1, arr2 + np.max(arr1) + 1, axis=0)

This gives you a list of arrays as you want.

print(list_of_arrs)
# [[0] [1] [2] [2] [2] [1] [0] [0] [2] [3] [5] [5] [5] [3] [5] [3] [4] [4]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM