0

I want to modify rows of numpy arrays stored in a list. length of my numpy arrays are not the same. I have several huge numpy arrays stored as list. This is my data (for simplicity I copied only a small list of array):

elements= [array([[971, 466, 697, 1, 15, 18, 28],
                 [5445, 4, 301, 2, 12, 47, 5]]),
           array([[5883, 316, 377, 2, 9, 87, 1]])]

Then, I want to replace the fourth column of each row with the last one and then delete the last column. I want to have the following result:

[array([[971, 466, 697, 1, 28, 18],
        [5445, 4, 301, 2, 5, 47]]),
 array([[5883, 316, 377, 2, 1, 87]])]

I tried the following code but it was not successful:

length=[len(i) for i in elements] # To find the length of each array
h=sum(length) # to find the total number of rows
for i in range (h):
    elements[:,[4,-1]] = elements[:,[-1,4]]
    elements=np.delete(elements,[-1],1)

I am facing the following error:

TypeError: list indices must be integers or slices, not tuple

I appreciate ay help in advance.

3 Answers 3

2

You can do it without loops but it's still slower (1.75 times on large data) than accepted solution:

counts = list(map(len, elements))
arr = np.concatenate(elements)
arr[:, 4] = arr[:, -1]
new_elements = np.split(arr[:,:-1], np.cumsum(counts)[:-1])

Concatenation is quite slow in numpy.

Sign up to request clarification or add additional context in comments.

Comments

1

A simple inefficient solution:

import numpy as np

elements= [np.array([[971, 466, 697, 1, 15, 18, 28],
                     [5445, 4, 301, 2, 12, 47, 5]]),
           np.array([[5883, 316, 377, 2, 9, 87, 1]])]

new_elements = list()
for arr in elements:
    arr[:, 4] = arr[:, -1]
    new_elements.append(arr[:, :-1])

The new list output is:

new_elements
Out[11]: 
[array([[ 971,  466,  697,    1,   28,   18],
        [5445,    4,  301,    2,    5,   47]]),
 array([[5883,  316,  377,    2,    1,   87]])]

1 Comment

Dear @Mathieu, Thanks for your solution. It is exactly what I wanted. I do appreciate it.
1

Try this one

p=[]
for x in range(len(elements)):
    for y in range(len(elements[x])):
         p.append(list(elements[x][y][:4])+[elements[x][y][-1]]+[elements[x][y][-2]])
print(p)

[[971, 466, 697, 1, 28, 18],
[5445, 4, 301, 2, 5, 47],
[5883, 316, 377, 2, 1, 87]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.