Python之路【第二篇:Python进阶】

一:set的用法分析:

  1、set是一个无序且不重复的集合,只由类语法创建,例如:

    s1 = set();

  2、set的优点:访问速度快,天生是用来解决重复问题。

  3、set的用法解析:

    •add():在集合中添加元素;

1 #!/usr/bin/env python
2 s1 = set([1,2,3,])
3 s1.add(4)
4 print(s1)
5 (1,2,3,4)
set

    •difference():做差集运算,原集合不更新;

1 #!/usr/bin/env python
2 s1 = ([1,2,3,])
3 s2 = s1.difference([2,3,4])    #在s1中循环,剔除交集部分,剩余元素返回值赋予s2
4 print(s2)
5 (1)
set

    •difference_update():做差集运算,原集合更新;

1 #!/usr/bin/env python
2 s1 = set([1,2,3,])
3 s1.difference_update([2,3,4,])
4 print(s1)
5 (1)
set

    •intersection():取交集;不更新原集合;

1 #!/usr/bin/env python
2 s1 = set([1,2,3,])
3 s2 = s1.intersection([2,3,4,])
4 print(s2)
5 (2,3)
set

    •intersection_update():取交集,更新原集合;

1 #!/usr/bin/env python
2 s1 = ([1,2,3,])
3 s2 = s1.intersection_update([2,3,4])
4 print(s1)
5 (2,3)
6 print(s2)
7 None
set

    •isdisjoint():是否有交集,返回值为True,False;

1 #!/usr/bin/env python
2 s1 = ([1,2,3])
3 s2 = ([2,3,4])
4 s1.isdisjoint(s2)
5 True
set

    •issubnet():是否为子集,返回值为True,False;

1 #!/usr/bin/env python
2 s1 = set([1,2,])
3 s2 = set(1,2,3,4)
4 s1.issubnet(s2)
5 True
set

    •issuperset():是否为父集,返回值为True,False;

1 #!/usr/bin/env python
2 s1 = set([1,2,3,])
3 s2 = set([1,2,3,4,5])
4 s2.issuperset(s1)
5 True
set

    •pop():对集合进行随机元素的删除(不需要参数),并且获得这个元素;

1 #!/usr/bin/env python
2 s1 = set([1,2,3,])
3 s2 = s1.pop()
4 print(s1)
5 (1,2)/(1,3)/(2,3)
6 print(s2)
7 (3)/(2)/(1)
set

    •remove():对集合元素有目标删除(需要参数),没有返回值;

1 #!/usr/bin/env python
2 s1 = set([1,2,3])
3 s2 = s1.remove([1])
4 print(s1)
5 (2,3)
6 print(s2)
7 None
set

    •symmetric_difference():对称差;

1 #!/usr/bin/env python
2 s1 = set([1,2,3,4])
3 s2 = set([3,4,5,6])
4 s1.symmetric_difference(s2)
5 (1,2,5,6)
set

 

二、collections系列:

  1、Counter计数器:对字典类的补充,用于追踪值出现的次数;功能上是对字典的继承,对字典加工处理;使用Counter功能需要导入collections模块,再通过Counter建立对象;

    •Counter的使用

1 #!/usr/bin/env python
2 import collections
3 a = collections.Counter('qwerq')
4 print(a)
5 Counter({'q':2,'w':1,'e':1,'r':1})
Counter

    •most_common(n):只拿前n位:

1 #!/usr/bin/env python
2 import collections
3 a = collections.Counter('qwerq')
4 a.most_common(2)
5 [('q',2),('w',1)]
Counter

    •elements():循环Counter中的所有内容;

 1 #!/usr/bin/env python
 2 import collections
 3 a = collections.Counter('qwerq')
 4 for i in a.elements():
 5     print(i)
 6 q
 7 q
 8 w
 9 e
10 r
Counter

    •items():统计元素出现的次数;

1 #!/usr/bin/env python
2 import collections
3 a = collections.Counter('qwerq')
4 for i in a.items():
5     print(i)
6 ('q',2)
7 ('w',1)
8 ('e',1)
9 ('r',1)
Counter

   2、有序字典:OrderedDict;通过列表来完成有序操作; 

1 #!/usr/bin/env python
2 import collections
3 dic = collections.OrderedDict()
4 dic['k1']=1
5 dic['k2']=2
6 dic
7 OrderedDict([('k1',1),('k2',2)])
OrderedDict

    •pop():加参数,挑着取值;

1 #!/usr/bin/env python
2 import collections
3 a = collections.OrderedDict()
4 a['k1']=1
5 a['k2']=2
6 b = a.pop('k1')
7 b
8 1
OrderedDict

    •popitem():不加参数,遵循后进先出的原则;

    •setdefault():设置默认值;

1 #!/usr/bin/env python
2 import collections
3 a = clooections.OrderdDict()
4 a.setdefault('k1',1)
5 a
6 OrderedDict([('k1',1)])
OrderedDict

    •update():更新原来的数据,传的是一个字典;

1 #!/usr/bin/env python
2 import collections
3 a = collections.OrderedDict()
4 a['k1']=1
5 a['k2']=2
6 a.update({'k2':222})
7 a
8 OrderedDict([('k1,1'),('k2',222)])
OrderedDict

      3、默认字典,创建一个字典默认值为list:

1 #!/usr/bin/env python
2 import collections
3 a = collections.defaultdict(list)
4 a['k1'].append('1les')
5 a
6 defaultdict((class 'list'),('k1':['alex']))
defaultdict

    4、namedtuple,可命名元组:

 1 #!/usr/bin/env python
 2 import collections
 3 MytupleClass = collections.namedtuple('MytupleCliass',['x','y','z'])
 4 #创建类
 5 obj = MytupleClass(1,2,3)
 6 #创建对象
 7 print(obj.x)
 8 print(obj.y)
 9 print(obj.z)
10 1
11 2
12 3
namedtuple

    5、deque,双向队列

    •append,往右添加;appendleft,往左添加;

1 #!/usr/bin/env python
2 import collections
3 a = collections.deque()
4 a.append(1)
5 a.appendleft(2)
6 a
7 deque([2,1])
deque

 

    

 

 

 

 

 

 

 

 

 

 

 

 

 

   

    

 

你可能感兴趣的:(Python之路【第二篇:Python进阶】)