以下所有内容均为作者原创,欢迎转载,但是转载时请注明出处!
set集合
set是一个无序且不重复的元素的集合。
常用方法:
a={1,2,3,4,5}
b={3,4,5,6,7}
a.add():添加元素
a.clear():清空元素
a.copy():浅copy
a.difference(b):同集合b比较,返回一个在b集合中没有的元素集合,a集合不变
a.difference_update(b):同集合b比较,a集合改变为 去掉所有跟b集合一样的元素 的集合
a.discard(指定的元素):去除a中指定的元素,如果元素不存在,则do nothing
a.intersection(b):返回a和b的交集,a不变
a.intersection_update(b):取a和b的交集,a改变为交集
a.isdisjoint(b):是否有和b有交集,没有交集返回True
a.issbuset(b):a是否是b的子集,是返回True
a.issuperset(b):a是否是b的父集,是返回True
a.pop():删除并返回一个随机的元素,不能指定元素,如果集合为空,返回KeyError
a.remove(指定元素):删除一个元素
a.symmetric_difference(b):返回a和b中不交叉的元素的集合,a不变
a.symmetric_difference_update(b):a集合变为a和b不交叉的元素的新集合
a.union(b):返回a和b的并集,a不变
a.update(b):a变为a和b的并集
collection-对字典扩展
使用collection需要手动导入,import collection
一、计数器(Counter)
计数器是对字典类型的补充,用于追踪值的出现次数,可以追终字符串、元组、列表中的元素
import collections a=collections.Counter() a.update('asdfasdfasdfasdfasdfgdfs') print(a) #结果如下 Counter({'f': 6, 'd': 6, 's': 6, 'a': 5, 'g': 1}) a.update([1,1,2,3,4,3,4,53,5,32,23,23,]) #结果如下: Counter({'d': 6, 'f': 6, 's': 6, 'a': 5, 1: 2, 3: 2, 23: 2, 4: 2, 32: 1, 2: 1, 'g': 1, 5: 1, 53: 1})方法说明:
还以上面代码中的a对象为例。
a.__missing__():默认方法,元素不存在,返回值为0
a.elements():列出所有元素
a.most_common():列出前n位最多的元素统计
a.copy():浅拷贝
a.update(元素):在原来元素的基础上新增元素
a.substract(元素):在原来元素的基础上减少参数中对应元素的数量
二、有序字典(orderedDict)
orderedDict是对字典类型的补充,他记住了字典元素的添加顺序。
orderedDict有序字典的创建原理其实就是python内部将key创建为一个列表,然后循环这个列表,去取对应的value值因为列表是有序的,所以读到的value也是有序的。
三、默认字典(defaultdict)
默认字典可以在创建字典时给字典中的value设置默认的数据类型,这样可以在以后访问字典时按字典中的默认顺序遍历。
a=collections.defaultdict(list)
这样就可以直接使用a.append()方法。
四、可命名元组(namedtuple)
namedtuple的功能是可以让元组中的元素访问时直接使用元素的名字而非下标访问。是对元组的扩展
使用示例:
mytuple=collections.namedtuple('tuplename',['x','y','z'])
实际上mytuple相当于一个class,还需要实例化:
import collections mytuple=collections.namedtuple('tuplename',['x','y','z']) a=mytuple(1,2,4) print(a.x) print(a.y) print(a.z)五、队列(双向deque,单向quque)
deque:两端都可以操作的序列,可以在序列前后中间任意位置执行添加或删除.有点儿像list
创建一个队列
d=deque()
队列的方法有:
def append(self, *args, **kwargs): # real signature unknown """ Add an element to the right side of the deque. """ pass def appendleft(self, *args, **kwargs): # real signature unknown """ Add an element to the left side of the deque. """ pass def clear(self, *args, **kwargs): # real signature unknown """ Remove all elements from the deque. """ pass def count(self, value): # real signature unknown; restored from __doc__ """ D.count(value) -> integer -- return number of occurrences of value """ return 0 def extend(self, *args, **kwargs): # real signature unknown """ Extend the right side of the deque with elements from the iterable """ pass def extendleft(self, *args, **kwargs): # real signature unknown """ Extend the left side of the deque with elements from the iterable """ pass def pop(self, *args, **kwargs): # real signature unknown """ Remove and return the rightmost element. """ pass def popleft(self, *args, **kwargs): # real signature unknown """ Remove and return the leftmost element. """ pass def remove(self, value): # real signature unknown; restored from __doc__ """ D.remove(value) -- remove first occurrence of value. """ pass def reverse(self): # real signature unknown; restored from __doc__ """ D.reverse() -- reverse *IN PLACE* """ pass
d = deque(maxlen=30)可以限制序列的长度quue:先进先出
定义一个单向队列
import queue
myqueue = Queue.Queue(maxsize = 10)Queue.Queue类即是一个队列的同步实现。队列长度可为无限或者有限。可通过Queue的构造函数的可选参数maxsize来设定队列长度。如果maxsize小于1就表示队列长度无限。
a=queue.Queue()a.empty():判断队列是否为空
a.full():判断队列是否为满
a.pop():从队列头删除一个元素并返回
a.put(i):放一个值到队列的尾部
三、深浅copy
一般数据类型中的copy访问(obj.copy())为浅copy,数字和字符串不受深浅copy影响。
深copy需要导入copy模块
import copy
copy.deepcopy()
深浅copy其实可以这样理解,所谓浅拷贝就是对引用的拷贝,所谓深拷贝就是对对象的资源的拷贝。
浅copy后如果修改copy后的数据会对原数据同步更改,深copy不会。
实际上是浅copy只是copy了第一层地址,深copy是重新开辟内存空间,所以深copy后修改数据不会对copy源的数据造成影响。
如果日后有遗忘可参考:http://www.cnblogs.com/zhaojianbo/p/5137026.html