Strip all non-numeric characters (except for “.”) from a string in Python

后端 未结 6 1514
死守一世寂寞
死守一世寂寞 2020-12-07 22:26

I\'ve got a pretty good working snippit of code, but I was wondering if anyone has any better suggestions on how to do this:

val = \'\'.join([c for c in val          


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 23:20

    Another 'pythonic' approach

    filter( lambda x: x in '0123456789.', s )

    but regex is faster.

提交回复
热议问题