列表推导目的是减少将一个列表转换为另一个列表时所需编写的代码量,其功能也能用列表迭代完成

1.根据要求创建列表threes_and_fives(列表值包括1到15中能够被3或者5正常的数)

threes_and_fives=[x for x in range(1,16) if x%3==0 or x%5==0]

2.lambda表达式实例(剔除掉列表中的”X“)

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"
message=filter(lambda x: not x=="X",garbled)
print message

 

相关文章:

  • 2021-07-21
  • 2021-08-05
  • 2021-09-21
  • 2022-12-23
  • 2022-01-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-10
  • 2022-12-23
  • 2021-10-11
  • 2021-11-13
  • 2022-02-01
  • 2021-04-29
相关资源
相似解决方案