Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, last element cut

Tags:

python

Well problem was that my original input is:

x=[['1', '7', 'gg'], ['1.5', '8', 'as']...]

I need just cut last element in every row.

I try to cut last element in matrix with that:

hl=x[:,:-1]

BUT THIS IS NOT WORKING, so I try in this way:

kl=array(x) 
hl=x[:,:-1]

Now I get:

[['1' '7']
 ['1.5' '8']]

Instead of: [['1' , '7'] ['1.5' , '8']]

Any solution?

like image 381
thaking Avatar asked Mar 16 '26 20:03

thaking


2 Answers

>>> x=[['1', '7', 'gg'], ['1.5', '8', 'as']]
>>> [s[:-1] for s in x]
[['1', '7'], ['1.5', '8']]
like image 185
tuscias Avatar answered Mar 19 '26 09:03

tuscias


Try this:

for row in x:
    del row[-1]
like image 32
Jason Orendorff Avatar answered Mar 19 '26 10:03

Jason Orendorff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!