python - How to check float string? -


I have a list containing irregular words and float numbers, I would like to delete all the list From the float number, but first I have to find a way to find them. I know str.isdigit () can differentiate the number, but this float can not work for numbers how to do this?

My code is like this:

  my_list = ['Fun', '3.25', '4.222', 'Quiet', '82 .356 ',' Go ',' Foo ',' 255.224 '] in my_list: if i.isdigit () == true: my_list.pop (i) # can not work, i.isdigit return false  

Use an exception handling and an understanding of the list.

  & gt; & Gt; & Gt; Def is_float (x): ... Try: ... float (x) ... Returns except True ... ValueError: ... return False & gt; & Gt; & Gt; Lis = ['Fun', '3.25', '4.222', 'Quiet', '82 .356 ',' Go ',' Foo ',' 255.224 ']> gt; & Gt; & Gt; [For X, if not in LIS is_float (x)] ['funny', 'cool', 'go', 'foo']  

modify the same list item Use slice assignment for:

  & gt; & Gt; & Gt; Lis [:] = [If X is not for X then then_flat (x)]> gt; & Gt; & Gt; Lis ['Fun', 'Cool', 'Go', 'Foo']  

Comments