[Python] python filter函数使用范例演示 →→→→→进入此内容的聊天室

来自 , 2019-07-16, 写在 Python, 查看 99 次.
URL http://www.code666.cn/view/26cd8eca
  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']
  18. #//python/8321

回复 "python filter函数使用范例演示"

这儿你可以回复上面这条便签

captcha