0

These is three lines of a matrix:

16  disk    11  10.29   4.63    30.22 
79  table   11  20.49   60.60   20.22 
17  disk    11  22.17   0.71    10.37 

I want to add each three lines in one row and i want to ignore string column. So the result of the first row on new matrix should be this:

16 11  10.29   4.63    30.22 79 11  20.49   60.60   20.22  17 11  22.17   0.71    10.37 

What i did for these 3 lines:

y=[]
for i in range (3):
     y=append(y, X[i,0:0 and 2:])

But it doesn't work. Could you please guide me?

Talia
  • 2,947
  • 4
  • 17
  • 28

1 Answers1

0

What about:

A = [16,  'disk',    11,  10.29,   4.63,    30.22, 
79,  'table',   11,  20.49,   60.60,   20.22, 
17,  'disk',    11,  22.17,   0.71,    10.37 ]

[item for item in A if not isinstance(item,str)]

In case A is a numpy matrix:

import re
RE_D = re.compile('\d')
[subitem for item in A.tolist() for subitem in item if RE_D.search(subitem)]

or maybe:

[subitem for item in A.tolist() for subitem in item if not subitem.isalpha()]
Moritz
  • 5,130
  • 10
  • 40
  • 81
  • I found this interesting solution: http://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-float-in-python – Moritz Oct 24 '15 at 19:43
  • Could you please explain about the code that you write? i can't understand how does it work? – Talia Oct 24 '15 at 22:12
  • you have your matrix A, the generator loops over each entry and if the entry is not a string, it is yielded as list. Google `generators`. – Moritz Oct 24 '15 at 22:36