集合
集合的定义:
集合是一个无序的,不重复的数据组合set = {}set = {1,2,3}set = {1,2,3,1,2,3}set = {1,2,3,’hello’}set = {1,2,3,’hello’,(1,2,3)}列表不能够存在于集合中:
In [2]:
set = {1,2,3,'hello',(1,2,3),[1,2,3]}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-9b6dfb40f232> in <module>()
----> 1 set = {1,2,3,'hello',(1,2,3),[1,2,3]}
TypeError: unhashable type: 'list'
In [3]:
集合的关系测试:
a = {
1,
2,
3}
b = {
3,
4,
5}
c = {
5,
6}
a.intersection(b)
"集合a与b的交集"
a.uniou(b)
"集合a与b的并集"
a.difference(b)
"集合a与b的差集"
b.difference(
a)
"集合b与a的差集"
a.symmetric_difference(b)
"集合a与b的对等差分"
a.issubset(b)
"集合a是否是集合b的子集,是返回True,不是返回False"
b.issubset(
a)
"集合b是否是集合a的子集,是返回True,不是返回False"
a.issuperset(b)
"集合a是否是集合b的父集,是返回True,不是返回False"
b.issuperset(
a)
"集合b是否是集合a的父集,是返回True,不是返回False"
a.isdisjoint(b)
"集合a与集合b是否有交集,有返回False,没有返回True"
集合关系的简化操作:
交集:
a&b
并集:
a|b
差集:
a-b
b-
a
对等差分:
a^b
集合的增删改查:
增加: 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]
判断是否可以迭代:
输入以下命令判断是否可迭代,可以返回True,不可以返回False
from collections
import Iterable
isinstance(
"hello",Iterable)
列表生成式
列表生成式,其实就是生成列表的一种方式,是python内置的方法;简单的列表生成式 [ i*i for i in range(10) ] # 返回0-9的平方的一个列表;
In [
3]: [i*i
for i
in range(
10)]
Out[
3]: [
0,
1,
4,
9,
16,
25,
36,
49,
64,
81]
In [
4]:
[ fun(i) for i in l] # 对列表的每个元素执行某个函数操作;
for…if的嵌套 [ i for i in range(1,11) if i%2==0] # 返回1-10之间所有的偶数
In [
5]: [i
for i
in range(
1,
11)
if i%
2==
0]
Out[
5]: [
2,
4,
6,
8,
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);就是你需要多少,它就生成多少;
创建生成器的方法:
将列表生成式的[]改为(),
In [
9]: g = (i
for i
in xrange(
1,
5))
In [
10]: g
Out[
10]: <generator
object <genexpr> at
0x7f4818033960>
在定义的函数中加入yield关键字;
查看生成器元素的方式:
使用生成器的next方法;(不常用),将生成的元素取完以后会报错:
In [
9]: g = (i for i
in xrange(
1,
5))
In [
10]: g
Out[
10]: <generator object <genexpr> at
0x7f4818033960>
In [
11]: g
.next()
Out[
11]:
1
In [
12]:
In [
12]: g
.next()
Out[
12]:
2
In [
13]: g
.next()
Out[
13]:
3
In [
14]: g
.next()
Out[
14]:
4
In [
15]: g
.next()
---------------------------------------------------------------------------
StopIteration Traceback (most recent
call last)
<ipython-input-
15-d7e53364a9a7>
in <module>()
---->
1 g
.next()
StopIteration:
In [
16]:
生成器是可迭代的对象,直接通过for循环查看;如果用next方法将元素取完以后,用for循环时看不到,是因为里面的元素已经被取出,用for循环查看需要重新生成元素:
In [
28]: g = (i
for i
in xrange(
1,
5))
In [
29]: g.
next()
Out[
29]:
1
In [
30]: g.
next()
Out[
30]:
2
In [
31]: g.
next()
Out[
31]:
3
In [
32]: g.
next()
Out[
32]:
4
In [
33]: g.
next()
StopIteration Traceback (most recent call last)
<ipython-input-
33-d7e53364a9a7>
in <module>()
StopIteration:
In [
34]:
for i
in g:
print i
....:
In [
35]:
重新生成才可用for循环看到:
In [
16]: g = (i
for i
in xrange(
1,
5))
In [
17]:
for i
in g:
....: print i,
....:
1 2 3 4
In [
18]:
内置高阶函数
能接收函数做参数的函数就是高阶函数
1.map()函数:
接收一个函数和一个列表,并通过把函数依次作用在列表中的每个元素上得到新的列表返回,(map函数不改变原有列表,而返回新的类列表)功能:
运用map()函数,可以把一个列表转换为另一个列表,只需要传入转换函数列表中可以包含的元素是任意类型,只要传入的函数能够处理这种数据类型,map()函数都可以处理 示例:
In [
4]: def
F(x):
...:
return x*x
...:
In [
5]: map(
F,range(
1,
11))
Out[
5]: [
1,
4,
9,
16,
25,
36,
49,
64,
81,
100]
2.reduce()函数: 接收一个函数和一个列表,reduce()函数传入的函数必须接收两个参数,reduce()对列表中的每个元素反复调用函数,最终返回结果 示例:
In [
6]: def add(x,y):
...:
return x+y
...:
In [
7]: reduce(add,range(
1,
11))
Out[
7]:
55
3.filter()函数 接收一个函数和一个列表,接收的函数对每个元素进行判断,返回Ture或False,filter()根据判断结果自动过滤掉不符合条件的元素 返回由符合条件元素组成的新的列表 示例: 将1-10中的偶数过滤出来
In [
8]: def ou(x):
...:
if x %
2 ==
0:
...:
return True
...:
In [
9]: filter(ou,range(
1,
11))
Out[
9]: [
2,
4,
6,
8,
10]
4.sorted()函数 sorted()函数可以对一个列表进行排序 示例:
In [
12]: l = [
1,
7,
2,
5]
In [
13]: sorted(l)
Out[
13]: [
1,
2,
5,
7]
sorted()函数也可以接收一个比较函数来实现自定义排序,比较函数的定义是,传入两个待比较的元素x,y,如果x应该排在y的前面,返回-1,如果x应该排在y的后面,返回1,如果x和y相等,返回0 所以,如果要实现倒叙,只需要编写一个reversed_cmp函数:
In [
14]:
def reversed_cmp(x,y):
....:
if x > y:
....:
return -
1
....:
if x < y:
....:
return 1
....:
if x == y:
....:
return 0
....:
In [
15]: sorted(l,reversed_cmp)
Out[
15]: [
7,
5,
2,
1]
sorted()函数也可以对字符串进行排序,字符串默认按照ASCII大小来比较:
In [
17]: l = [
"hello",
"SA",
"ok"]
In [
18]: sorted(l)
Out[
18]: [
'SA',
'hello',
'ok']
点击”基本框架”可查看脑图:
基本框架