Skip to content

Instantly share code, notes, and snippets.

@ayuLiao
Created February 25, 2019 11:54
Show Gist options
  • Save ayuLiao/7ddbcbd2be00ec2a58db7ab89604ef22 to your computer and use it in GitHub Desktop.
Save ayuLiao/7ddbcbd2be00ec2a58db7ab89604ef22 to your computer and use it in GitHub Desktop.
>>>crazystring = ‘dade142.;!0142f[.,]ad’
>>> filter(str.isdigit, crazystring) ‘1420142’ #只保留数字
>>> filter(str.isalpha, crazystring) ‘dadefad’ #只保留字母
>>> filter(str.isalnum, crazystring) ‘dade1420142fad’ #只保留字母和数字
#如果想保留数字0-9和小数点’.’ 则需要自定义函数
>>> filter(lambda ch: ch in ‘0123456789.’, crazystring) ‘142.0142.’
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment