2

i have problem like this..

arr = [['1' '2' '']['3' '4' '']['5' '6' '']]

then, how do I delete the last element - array[n][2] I have tried and searching google a lot but didn't solve it please help me to solve this problem thank you...

arr = [['1' '2']['3' '4']['5' '6']]
3
  • 1
    Please format the array correctly! Also show what you have tried in terms of code! Commented May 4, 2019 at 4:06
  • Do you want to remove the last one or empty one? Commented May 4, 2019 at 4:13
  • Possible duplicate of delete last item in all rows and columns numpy.ndarray Commented May 4, 2019 at 5:40

4 Answers 4

3

Use numpy.delete(arr,-1,1)

arr = numpy.array([['1','2',''],['3','4',''],['5','6','']])
arr = numpy.delete(arr,-1,1)

The result will be:

[['1' '2']
['3' '4']
['5' '6']]
Sign up to request clarification or add additional context in comments.

Comments

1

I would use a numpy.apply_along_axis to remove the last element from each sublist

import numpy as np
arr = np.array([['1', '2', ''],['3' ,'4', ''],['5', '6' ,'']])

#Slice each sublist to remove the last element using apply_along_axis
arr = np.apply_along_axis(lambda x: x[:-1], 1, arr)
print(arr)

The output will be

[['1' '2']
 ['3' '4']
 ['5' '6']]

7 Comments

Hold on. There is a numpy tag.
Even if it was a numpy array, map will still work @Austin, I updated my answer to reflect that! I didn't understand the reason of downvote though?
I agree it works, but OP is asking for numpy way to solve it.
Sure thing @Austin , I used a numpy function for it, please check and if it looks good, please consider upvoting :)
@Austin But, There is no array with numpy in their code. arr is just list. I think numpy is not mandatory if we can solve it in standard library.
|
1
In [415]: arr = np.array([['1', '2', ''],['3' ,'4', ''],['5', '6' ,'']]) 
     ...:                                                                            
In [416]: arr                                                                        
Out[416]: 
array([['1', '2', ''],
       ['3', '4', ''],
       ['5', '6', '']], dtype='<U1')

Just take a column slice:

In [417]: arr[:, :-1]                                                                
Out[417]: 
array([['1', '2'],
       ['3', '4'],
       ['5', '6']], dtype='<U1')

This produces a new array, but that's normal with numpy. Most numpy actions, especially ones that change size/shape, do that.

The OP display of arr is consistent with it being numpy array. However for copy-n-paste the repr display is better.

In [418]: print(arr)                                                                 
[['1' '2' '']
 ['3' '4' '']
 ['5' '6' '']]

Comments

0

I have an idea to check '' with len().

arr = [['1', '2', ''],['3', '4', ''],['5', '6', '']]
arr = [[a for a in ar if len(a) > 0] for ar in arr]
print (arr)

The result is:

[['1', '2'], ['3', '4'], ['5', '6']]

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.