1. 元素去重

data = [1,2,3,4,4,5,5,6]
res = set(data)

2. 元素去重排序

res = sorted(set(data))

2. 打印重复的元素

res = [x for x in data if data.count(x) > 1]

3. 阶乘

num = 10
res = reduce(lambda x, y: x*y, range(num+1)[1:])

6. 列表推导式

origin = ['a', 'b', 'c', 'z']

print [x for x in origin if x is not 'z']    # ['a', 'b', 'c']
print [x for x in list(enumerate(origin, 5)) if x is not 'z'] # [(5, 'a'), (6, 'b'), (7, 'c'), (8, 'z')]

7. 字典推导式

origin = ['a', 'b', 'c', 'z']

res = {x: origin.index(x) for x in origin}    # {'a': 0, 'c': 2, 'b': 1, 'z': 3}
print type(res) # <type 'dict'>

8. 集合推导式

origin = ['a', 'b', 'c', 'z']

print {x*2 for x in origin}    # set(['aa', 'cc', 'zz', 'bb'])

9. 生成器推导式(有点像元组)

origin = ['a', 'b', 'c', 'z']

res = (x*2 for x in origin)
print res # <generator object <genexpr> at 0x7fe2ca76d640>
print type(res) # <type 'generator'>

10.生成器和其它集合的转换

def test():
for i in [1, 2,3]:
yield i res = test()
print res # <generator object test at 0x7f404d077640>
res1 = set(res)
print res1 # set([1, 2, 3])
res2 = list(res)
print res2 #为空 生成器是有状态的,不能反复调用
res3 = tuple(res)
print res3 #为空 生成器是有状态的,不能反复调用

python 基础——常用功能片段的更多相关文章

  1. Python 基础 常用运算符

    Python 基础 常用运算符 计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算术运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算. 今天我们暂只学习 算术运算. ...

  2. Python自动化 【第五篇】:Python基础-常用模块

    目录 模块介绍 time和datetime模块 random os sys shutil json和pickle shelve xml处理 yaml处理 configparser hashlib re ...

  3. python基础--常用模块与面向对象基础

    1常用模块 1.1 xml xml是实现不同语言或程序之间进行数据交换的协议 xml的格式如下: <?xml version="1.0"?> <data> ...

  4. Python 基础 常用模块

    Python 为我们提供了很多功能强大的模块,今天就主要使用的到的模块进行整理,方便后面来翻阅学习. 一.时间模块 在时间模块中我们重点介绍几种自己常用的功能,主要方便我们按照自己想要的方式获取时间 ...

  5. python基础----常用模块

    一 time模块(时间模块)★★★★                                                      时间表现形式 在Python中,通常有这三种方式来表示时 ...

  6. python学习:python的常用功能示例2

    1. python 写入txt with open("douban.txt","w") as f: f.write("这是个测试!") fi ...

  7. Git基础常用功能

    一.安装 具体查看 安装Git 二.使用 基础知识 工作区(Workspace):就是你在电脑里能看到的项目目录. 暂存区(Index / Stage):临时存放更改的地方,使用命令"git ...

  8. Python基础-常用模块OS

    模块:一个python文件就是一个模块,模块分三种: 1,标准模块,也就是python自带的模块,例如import  time,random,string等等 2,第三方模块,这种模块需要自己安装才能 ...

  9. python基础--常用的模块(collections、time、datetime、random、os、sys、json、pickle)

    collection模块: namedtuple:它是一个函数,是用来创建一个自定义的tuple对象的,并且规定了tuple元素的个数,并可以用属性而不是索引来引用tuple的某个元素.所以我们就可以 ...

随机推荐

  1. 100个直接可以拿来用的JavaScript实用功能代码片段(转载)

    把平时网站上常用的一些实用功能代码片段通通收集起来,方面网友们学习使用,利用好的话可以加快网友们的开发速度,提高工作效率. 目录如下: 1.原生JavaScript实现字符串长度截取2.原生JavaS ...

  2. 在WindowsServer2008服务器上安装SQLServer2008R2

    登录服务器 使用远程桌面登录Windows Server 2008   安装前的准备工作 下载SQL Server安装程序 下载Microsoft SQL Server2008 R2 RTM - Ex ...

  3. C#用正则表达式 获取标签的属性或值

    整理两个 在C#中,用正则表达式 获取网页源代码标签的属性或值的方法 : 1.获取标签中的值: string str="<a href=\"www.csdn.net\&quo ...

  4. 如何加入自定义WebControl

    http://www.screencast.com/users/Dennis.Garavsky/folders/Default/media/c75b4ec6-1641-4f82-936e-39360d ...

  5. 本地存储(cookie&sessionStorage&localStorage)

    好文章,最全面.就查它吧:https://segmentfault.com/a/1190000004556040 1.DOM存储:https://developer.mozilla.org/zh-CN ...

  6. android自定义相册 支持低端机不内存溢出

    1 之前在网上看的自定义相册很多时候在低端机都会内存溢出开始上代码把 首先我们要拿到图片的所有路径 cursor = context.getContentResolver().query( Media ...

  7. 【转】android出现注: 某些输入文件使用或覆盖了已过时的 API。 注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。 注: 某些输入文件使用了未经检查或不安全的操作。 注

    使用Android studio打包应用程序出现如下错误: 注: 某些输入文件使用或覆盖了已过时的 API. 注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译. 注: 某些 ...

  8. iOS 7 Pushing the Limits Notes - create a layer like notification center's or control center's background

    Problem: How to create a layer that looks like your notification center's or control center's backgr ...

  9. hdu 5265 pog loves szh II STL

    pog loves szh II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  10. HTML5 API——无刷新更新地址 history.pushState/replaceState 方法

    尽 管是上面讲到的<JavaScript高级程序设计>(第二版)中提到,BOM中的location.path/query…… (window.location)在通过JavaScript更 ...