Python filter()函数用法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python filter()函数用法相关的知识,希望对你有一定的参考价值。
Shows usage of the Python filter() function
# Suppose you have a list of people's first names. You want to reduce the list down to only those people whose first names start with "C". people = ['Amy', 'Alice', 'Bobby', 'Charlie', 'Connie', 'David'] # You would create a callback function that would look something like this: def starts_with_c(name): if name[0] == "C": return True return False # So then, you would run the filter function starts_with_c_list = filter(starts_with_c, people) print starts_with_c_list # prints ['Charlie', 'Connie']
以上是关于Python filter()函数用法的主要内容,如果未能解决你的问题,请参考以下文章
python map() filter() reduce()函数的用法以及实例