JiFu's Wiki JiFu's Wiki
首页
  • HTML
  • JavaScript
  • NodeJS
  • Vuejs
  • 微信小程序
  • Python
  • 数据库
  • 中间件
  • 算法
  • 软件工程
  • Wordpress
  • iOS开发
  • Android开发
  • Linux
  • Windows
  • MacOS
  • Docker
  • Vim
  • VSCode
  • Office
  • 其他
  • Photoshop
  • Sketch
  • Mac
  • 游戏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
首页
  • HTML
  • JavaScript
  • NodeJS
  • Vuejs
  • 微信小程序
  • Python
  • 数据库
  • 中间件
  • 算法
  • 软件工程
  • Wordpress
  • iOS开发
  • Android开发
  • Linux
  • Windows
  • MacOS
  • Docker
  • Vim
  • VSCode
  • Office
  • 其他
  • Photoshop
  • Sketch
  • Mac
  • 游戏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 数据库

  • Python

    • Python介绍
    • 国内pip更换豆瓣的pypi源
    • sys.stdout.write实现Python控制台实时刷新打印
    • 发布你自己的轮子 - PyPI打包上传实践
    • Seaborn库绘制了17个超好看图表
    • Python脚本打包exe Auto-py-to-exe
    • Build-in

      • Python build-in functions
      • Python build-in functions - List
        • overview
        • explain
        • example
        • overview
        • explain
        • example
        • overview
        • explain
        • example
        • overview
        • explain
        • example
      • Python build-in functions - collections
      • Python build-in module - os
      • Python多线程和锁
    • Flask

    • Libaray

  • 中间件

  • 算法

  • 软件工程

  • Wordpress

  • 后端技术
  • Python
  • Build-in
JiFu
2023-10-18
目录

Python build-in functions - List

# map()

# overview

Query or sets dynamic values of the specified option(s) in style.

Each key in kw is an option and each value should be a list or a tuple (usually) containing statespecs grouped in tuples, lists, or something else of your preference. A statespec is a compound of one or more states and then a value.

# explain

map()方法接受两个参数,一个是函数,一个是序列list,map将传入的函数依次作用到序列的每个元素,然后返回新的list。

# example

def f(x):
    return x * x
map(f, [1, 2, 3, 4])
[1, 4, 9, 16]
1
2
3
4

# reduce()

# overview

Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned.

# explain

reduce()将一个函数作用于一个序列list上,必须接收两个参数,reduce将上一个操作的结果作为第一个参数与序列下一个参数参与运算。

# example

def add(x, y):
    return x + y
print reduce(add, [1, 2, 3])
6
1
2
3
4

# filter()

# overview

Returns an instance of the Filter class. If name is specified, it names a logger which, together with its children, will have its events allowed through the filter. If name is the empty string, allows every event.

# explain

filter()接收一个函数和一个序列List,filter依次将传入函数依次作用于序列List中的每一个元素,并根据函数返回值True、False判定是否需要保留该元素。该方法返回一个新序列List。

# example

# 利用filter,保留序列List中偶数
def is_odd(x):
    if x % 2 == 0:
        return True
    else:
    return False
filter(is_odd, [1, 2, 3, 4, 5])
[2, 4]

# 利用filter,删除素数
def is_prime(x):
    for i in xrange(2, x):
        if x % i == 0:
            return False
    return True
filter(is_prime, xrange(100))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# sorted

# overview

Return a new sorted list from the items in iterable. The optional arguments cmp, key, and reverse have the same meaning as those for the list.sort() method (described in section Mutable Sequence Types).

cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.

key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None (compare the elements directly).

reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

# explain

sorted函数接收一个比较函数实现自定义排序。

# example

sorted([5, 9, 1, 3])
[1, 3, 5, 9]

def reversed_cmp(x, y):
    if x > y:
        return -1
    if x < y:
        return 1
    return 0


sorted([5, 9, 1, 3], reversed_cmp)
[9, 5, 3, 1]
1
2
3
4
5
6
7
8
9
10
11
12
13
上次更新: 2024/08/11, 01:59:03
Python build-in functions
Python build-in functions - collections

← Python build-in functions Python build-in functions - collections→

最近更新
01
Disable notification "to get future google chrome updates you'll need macos 10.13 or later" on mac
05-14
02
MacOS软件推荐
04-30
03
Debian Sway开发机安装手册
03-26
更多文章>
Theme by Vdoing | Copyright © 2019-2025 Ji Fu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式