python

  1.   filter():用于过滤序列,返回符合条件的结果

        filter(function, iterable)

        def is_odd(n):

          return n % 2 == 1

        newlist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 

        lambda 一起使用:filter(lambda x: x % 2 == 0, range(10))

      

        

  2.        map() 会根据提供的函数对指定序列做映射    

      map(function, iterable, ...)

       >>>def square(x) :            # 计算平方数
      ...     return x ** 2
      ...
      >>> map(square, [1,2,3,4,5])   # 计算列表各个元素的平方
      [1, 4, 9, 16, 25]
      >>> map(lambda x: x ** 2, [1, 2, 3, 4, 5])  # 使用 lambda 匿名函数
      [1, 4, 9, 16, 25]
 
      # 提供了两个列表,对相同位置的列表数据进行相加
      >>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
      [3, 7, 11, 15, 19]

  3.         sorted(iterable[, cmp[, key[, reverse]]]) 

    #sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。

 

    #list 的 sort 方法返回的是对已经存在的列表进行操作,无返回值,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。

    #reverse=True 降序,reverse=False 升序

    

相关文章:

  • 2021-05-21
  • 2021-09-27
  • 2021-12-05
  • 2022-12-23
  • 2021-11-18
  • 2021-09-27
猜你喜欢
  • 2022-12-23
  • 2022-03-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2021-11-17
相关资源
相似解决方案