美文网首页程序员
python的常用函数

python的常用函数

作者: 我只是我笔下的小丑 | 来源:发表于2018-12-14 17:46 被阅读0次

1. 排序

sorted(interable,key,reverse)

interable: 需要排序的对象;
key: 按照什么开始排序;
reverse: 是否逆序,默认从小到大,可设置为True。

sorted(word_count.items(),key=lambda x:x[1],reverse=True)

## 返回新列表
lst = [1,2,44,3,-1,0,54]
print(sorted(lst,key=lambda x:6-x))
print(lst)

#就地修改
list.sort(lst,key=lambda x: 6-x)
print(lst)
##
[54, 44, 3, 2, 1, 0, -1]
[1, 2, 44, 3, -1, 0, 54]
[54, 44, 3, 2, 1, 0, -1]

2. 过滤

#过滤出数列中能被3整除的数字

print(list(filter(lambda x: x%3==0, [1,9,55,150,-3,78,28,123])))

#过滤函数过滤可迭代对象的元素,返回一个迭代器。
[9, 150, -3, 78, 123]

3. 映射

#对多个可迭代对象的元素按照指示的函数进行映射

print(list(map(lambda x:2*x+1, range(5))))

print(dict(map(lambda x: (x%5,x) , range(500))))
[1, 3, 5, 7, 9]
{0: 495, 1: 496, 2: 497, 3: 498, 4: 499}
#### 1. 排序

## 返回新列表
lst = [1,2,44,3,-1,0,54]
print(sorted(lst,key=lambda x:6-x))
print(lst)

#就地修改
list.sort(lst,key=lambda x: 6-x)
print(lst)
##

#### 2. 过滤

#过滤出数列中能被3整除的数字

print(list(filter(lambda x: x%3==0, [1,9,55,150,-3,78,28,123])))

#过滤函数过滤可迭代对象的元素,返回一个迭代器。

#### 3. 映射

#对多个可迭代对象的元素按照指示的函数进行映射

print(list(map(lambda x:2*x+1, range(5))))

print(dict(map(lambda x: (x%5,x) , range(500))))
[54, 44, 3, 2, 1, 0, -1]
[1, 2, 44, 3, -1, 0, 54]
[54, 44, 3, 2, 1, 0, -1]
[9, 150, -3, 78, 123]
[1, 3, 5, 7, 9]
{0: 495, 1: 496, 2: 497, 3: 498, 4: 499}

4. zip

zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。

如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。

nums = ['flower','flow','flight']
for i in zip(*nums):
    print(i)
('f', 'f', 'f')
('l', 'l', 'l')
('o', 'o', 'i')
('w', 'w', 'g')

更多欢迎访问:http://www.mykernel.cn/

相关文章

  • Lesson 025 —— python 常用函数与深浅拷贝

    Lesson 025 —— python 常用函数与深浅拷贝 常用函数 map()map()是 Python 内置...

  • python常用函数

    Python常用函数 python中函数以两种形式呈现:一是可以自己定义的函数function,比如最常用的pri...

  • Python学习记录

    基本语法 Python lambda介绍 常用函数 python map( )函数用法map将传入的函数f依次作用...

  • Python精简入门学习(十一)

    Python精简入门学习之内置函数(上)-内置函数 -常用函数

  • python中常用的内建函数

    python中常用的内建函数 总结了python中常用的内建函数。 字符串内建函数 join 拼接字符串 完整的简...

  • Python学习

    第六天 Python常用内置函数Python3解释器中内置了69个常用函数,属于底层的函数,它们到处可用。有些对大...

  • python常用时间函数

    常用函数 日常写代码,经常用到时间相关的函数,以下整理了python常用的时间函数: 执行结果 此外datatim...

  • 2018-12-27

    Python 的字符串内建函数 Python 的字符串常用内建函数如下:

  • 从Excel到Python:最常用的36个Pandas函数

    从Excel到Python:最常用的36个Pandas函数 本文涉及pandas最常用的36个函数,通过这些函数介...

  • python学习2

    python学习二 一些常用函数

网友评论

    本文标题:python的常用函数

    本文链接:https://www.haomeiwen.com/subject/jaoghqtx.html