Python学习笔记 5--集合、函数
2017-08-30 21:46
453 查看
集合
集合的定义
由一个或多个确定的元素所构成的整体叫做集合集合是指具有某种特定性质的具体的或抽象的对象汇总成的集体,这些对象称为该集合的元素
集合的关系测试
s1 = {1, 2, 3} s2 = {2, 3, 4} s3 = {1, 2} s4 = {5} print "s1,s2,s3的交集:", s1.intersection(s2, s3) print "s1,s2,s3的交集:", s1 & s2 & s3 print "s1,s2,s3的并集:", s1.union(s2, s3) print "s1,s2,s3的并集:", s1 | s2 | s3 print "s1-s2的差集:", s1.difference(s2) print "s1-s2的差集:", s1 - s2 print "s1-s2-s3的差集:", s1.difference(s2, s3) print "s1-s2-s3的差集:", s1 - s2 - s3 print "s2-s1的差集:", s2.difference(s1) print "s1和s2的对等差分:", s1.symmetric_difference(s2) print "s1和s2的对等差分:", s1 ^ s2 print "s2和s1的对等差分:", s2.symmetric_difference(s1) # s.isdisjoint(s2) 如果有交集,返回False;否则,返回True; print "s1和s4是否没有交集:", s1.isdisjoint(s4) print "s3是否为s1的子集:", s3.issubset(s1) print "s1是否为s3的父集:", s1.issuperset(s3)
集合的增删改查:
增加: s.add(1), s.update(s1)删除:
s.remove(1), ## 删除存在的元素,如果不存在,直接报错; s.discard(1) ## 删除存在的元素,如果不存在,do nothing; s.pop(), ## 随即删除集合元素,不加任何参数;
复制: s1 = s.copy() #s和s1的id不同
清空: s.clear()
冻集(frozenset)
冻住的集合,不能增删改;frozenset(set)
set(frozenset)
高级特性
切片
迭代
任何可以迭代的对象都可以用for循环遍历;字典的迭代:默认是通过key进行迭代的;
for i in d: print i
字典的枚举:枚举只能返回返回两个值,一个是索引下标,一个是迭代的元素;
d = {1:"a",2:"b"} for i,j in enumerate(d): print i,j,d[j]
怎么去判断是否可迭代
from collections import Iterable isinstance("hello",Iterable)
列表生成式
列表生成式,其实就是生成列表的一种方式,是python内置的;简单的列表生成式
[ i*i for i in range(10) ] # 返回1-10平方的一个列表; [ fun(i) for i in l] # 对列表的每个元素执行某个函数操作;
for…if的嵌套
[ i*i for i in range(1,10) if i%2==0] # 返回1-10之间所有的偶数 [ i for i in range(1,10) if isprime(i)] # 返回1-10之间所有的质数,判断质数的函数是自定义的;
for…for…的嵌套
[ i+j for i in "abc" for j in "123"] # 返回"abc"和"123"的所有组合;
生成器
列表生成式可以直接创建一个列表,但列表容量受内存的限制;在Python中一边循环一边计算的机制,称为生成器(Generator);就是你需要多少,它就生成多少;
创建生成器的方法:
将列表生成式的[]改为();在定义的函数中加入yield关键字;
查看生成器元素的方式:
使用生成器的next方法;(不常用)生成器是可迭代的对象,直接通过for循环查看;
1, 1 , 1+1 max = 6 def fib(max): n,a,b = 0,1,1 while n < 6: print a a,b = b, a + b n = n + 1
高阶函数
内置的高阶函数
mapreduce
reduce(f,[1,2,3,4]) === > add(add(add(1,2),3),4) reduce(cheng,range(1,11))
** 求10的阶乘:
filter
In [15]: def is_oushu(n): ....: if n%2 == 0: ....: return True ....: else: ....: return False ....: In [16]: filter(is_oushu,range(1,11)) Out[16]: [2, 4, 6, 8, 10]
sorted()
n [20]: l = ["a","Ab","cd","C"] In [21]: sorted(l) Out[21]: ['Ab', 'C', 'a', 'cd'] In [22]: def cmp_ignore_case(s1,s2): ....: s1 = s1.lower() ....: s2 = s2.lower() ....: if s1 < s2: ....: return -1 ....: elif s1 > s2: ....: return 1 ....: else: ....: return 0 ....: In [23]: sorted(l,cmp_ignore_case) Out[23]: ['a', 'Ab', 'C', 'cd']
相关文章推荐
- 初学Python的学习笔记2----dist字典,set集合,声明函数,函数参数
- Python学习笔记 4--字典、集合、函数(python的乐高积木一)
- Python学习笔记:集合(set)有关函数
- python_模块与函数学习笔记
- Python学习笔记之集合类型(set, frozeset)
- python学习笔记--探索函数
- Python学习笔记整理(十二)Python的函数
- Python学习笔记(5):函数
- Python3.3 学习笔记4 - 函数 - lambda
- Python学习笔记:函数与类
- python学习笔记: 一些有用的文件操作函数
- Python 学习笔记 -- 变量、元组、列表、字典和集合
- python 学习笔记 -- 函数
- Python 学习笔记 - 9.函数(Function)
- Python3.3 学习笔记8 - 函数
- 学习python笔记——函数
- python学习笔记8-函数整理
- Python 学习笔记:需要仔细阅读一个函数
- Python3.3 学习笔记4 - 函数 - input & print
- python学习笔记——集合