Skip to content

Instantly share code, notes, and snippets.

@imbyron
Last active December 31, 2015 17:09
Show Gist options
  • Save imbyron/8017885 to your computer and use it in GitHub Desktop.
Save imbyron/8017885 to your computer and use it in GitHub Desktop.
python3中filter的用法
text = "aAsmr3idd4bgs7Dlsf9eAF"
#只保留数字
list(filter(str.isdigit, text))
#只保留字母
list(filter(str.isalpha, text))
def f(x):return x%2 != 0 and x%3 != 0
list(filter(f,range(1,50)))
#结果:[1, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49]
def f(y):return y!='a'
list(filter(f,"asassdsds123232"))
#结果:['s', 's', 's', 'd', 's', 'd', 's', '1', '2', '3', '2', '3', '2']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment