Python filter()函数用法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python filter()函数用法相关的知识,希望对你有一定的参考价值。

Shows usage of the Python filter() function
  1. # 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".
  2.  
  3. people = ['Amy', 'Alice', 'Bobby', 'Charlie', 'Connie', 'David']
  4.  
  5. # You would create a callback function that would look something like this:
  6.  
  7. def starts_with_c(name):
  8. if name[0] == "C":
  9. return True
  10. return False
  11.  
  12. # So then, you would run the filter function
  13.  
  14. starts_with_c_list = filter(starts_with_c, people)
  15. print starts_with_c_list
  16.  
  17. # prints ['Charlie', 'Connie']

以上是关于Python filter()函数用法的主要内容,如果未能解决你的问题,请参考以下文章

python中filter,reduce,map的用法

python map() filter() reduce()函数的用法以及实例

python filter函数

Python---高级函数map, filter, zip, enumerate等的用法

python中filter的用法

python中filter的用法