python中collections_python标准库之collections介绍

collections----容器数据类型

collections模块包含了除list、dict、和tuple之外的容器数据类型,如counter、defaultdict、deque、namedtuple、orderdict,下面将一一介绍。

Counter

初始化:

Counter 支持三种形式的初始化。它的构造函数可以调用序列,一个字典包含密钥和计数,或使用关键字参数映射的字符串名称。

importcollectionsprint (collections.Counter(['a', 'b', 'c', 'a', 'b', 'b']))print (collections.Counter({'a':2, 'b':3, 'c':1}))print (collections.Counter(a=2, b=3, c=1))

输出结果:

Counter({'b': 3, 'a': 2, 'c': 1})

Counter({'b': 3, 'a': 2, 'c': 1})

Counter({'b': 3, 'a': 2, 'c': 1})

空的Counter容器可以无参数构造,并采用update()方法进行更新

importcollections

c=collections.Counter()print ('Initial :', c)

c.update('abcdaab')print ('Sequence:', c)

c.update({'a':1, 'd':5})print ('Dict :', c)

输出结果:

Initial : Counter()

Sequence: Counter({'a': 3, 'b': 2, 'c': 1, 'd': 1})

Dict : Counter({'d': 6, 'a': 4, 'b': 2, 'c': 1})

访问计数:

当一个Counter被构造成功,它的值可以采用字典进行访问

importcollections

c= collections.Counter('abcdaab')for letter in 'abcde':print ('%s : %d' % (letter, c[letter]))

结果:

a : 3b :2c :1d :1e : 0

elements()方法可以返回一个包含所有Counter数据的迭代器

importcollections

c= collections.Counter('extremely')

c['z'] =0print(c)print (list(c.elements()))

Counter({'e': 3, 'm': 1, 'l': 1, 'r': 1, 't': 1, 'y': 1, 'x': 1, 'z': 0})

['e', 'e', 'e', 'm', 'l', 'r', 't', 'y', 'x']

most_common()返回前n个最多的数据

importcollections

c=collections.Counter('aassdddffff')for letter, count in c.most_common(2):print ('%s: %d' % (letter, count))

f: 4d:3

算法:

Counter实例支持聚合结果的算术和集合操作。

importcollections

c1= collections.Counter(['a', 'b', 'c', 'a', 'b', 'b'])

c2= collections.Counter('alphabet')print ('C1:', c1)print ('C2:', c2)print ('\nCombined counts:')print (c1 +c2)print ('\nSubtraction:')print (c1 -c2)print ('\nIntersection (taking positive minimums):')print (c1 &c2)print ('\nUnion (taking maximums):')print (c1 | c2)

C1: Counter({'b': 3, 'a': 2, 'c': 1})

C2: Counter({'a': 2, 'l': 1, 'p': 1, 'h': 1, 'b': 1, 'e': 1, 't': 1})

Combined counts:

Counter({'a': 4, 'b': 4, 'c': 1, 'l': 1, 'p': 1, 'h': 1, 'e': 1, 't': 1})

Subtraction:

Counter({'b': 2, 'c': 1})

Intersection (taking positive minimums):

Counter({'a': 2, 'b': 1})

Union (taking maximums):

Counter({'b': 3, 'a': 2, 'c': 1, 'l': 1, 'p': 1, 'h': 1, 'e': 1, 't': 1})

defaultdict

标准字典包括setdefault方法()获取一个值,如果值不存在,建立一个默认。相比之下,defaultdict允许调用者在初始化时预先设置默认值。

importcollectionsdefdefault_factory():return 'default value'd= collections.defaultdict(default_factory, foo='bar')print ('d:', d)print ('foo =>', d['foo'])print ('x =>', d['x'])

d: defaultdict(, {'foo': 'bar'})

foo=>bar

x=> default value

Deque

双端队列,支持从两端添加和删除元素。更常用的栈和队列是退化形式的双端队列,仅限于一端在输入和输出。

importcollections

d= collections.deque('abcdefg')print ('Deque:', d)print ('Length:', len(d))print ('Left end:', d[0])print ('Right end:', d[-1])

d.remove('c')print ('remove(c):', d)

Deque: deque(['a', 'b', 'c', 'd', 'e', 'f', 'g'])

Length:7Left end: a

Right end: g

remove(c): deque(['a', 'b', 'd', 'e', 'f', 'g'])

双端队列从左右两端插入数据

importcollections#右端插入

d =collections.deque()

d.extend('abcdefg')print ('extend :', d)

d.append('h')print ('append :', d)#左端插入

d =collections.deque()

d.extendleft('abcdefg')print ('extendleft:', d)

d.appendleft('h')print ('appendleft:', d)

extend : deque(['a', 'b', 'c', 'd', 'e', 'f', 'g'])

append : deque(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])

extendleft: deque(['g', 'f', 'e', 'd', 'c', 'b', 'a'])

appendleft: deque(['h', 'g', 'f', 'e', 'd', 'c', 'b', 'a'])

类似地,双端队列的元素可以从两端获取。

importcollectionsprint ('From the right:')

d= collections.deque('abcdefg')whileTrue:try:print(d.pop())exceptIndexError:break

print ('\nFrom the left:')

d= collections.deque('abcdefg')whileTrue:try:print(d.popleft())exceptIndexError:break

From the right:

g

f

e

d

c

b

a

From the left:

a

b

c

d

e

f

g

由于双端队列是线程安全的,在单独的线程中内容甚至可以从两端同时消费。

importcollectionsimportthreadingimporttime

candle= collections.deque(xrange(11))defburn(direction, nextSource):whileTrue:try:

next=nextSource()exceptIndexError:break

else:print ('%8s: %s' %(direction, next))

time.sleep(0.1)print ('%8s done' %direction)returnleft= threading.Thread(target=burn, args=('Left', candle.popleft))

right= threading.Thread(target=burn, args=('Right', candle.pop))

left.start()

right.start()

left.join()

right.join()

Left: 0

Right:10Right:9Left:1Right:8Left:2Right:7Left:3Right:6Left:4Right:5Left done

Right done

队列的另一个有用的功能是在任意方向旋转,通俗来讲就是队列的左移右移

importcollections

d= collections.deque(xrange(10))print ('Normal :', d)

d= collections.deque(xrange(10))

d.rotate(2)print ('Right rotation:', d)

d= collections.deque(xrange(10))

d.rotate(-2)print ('Left rotation :', d)

Normal : deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Right rotation: deque([8, 9, 0, 1, 2, 3, 4, 5, 6, 7])

Left rotation : deque([2, 3, 4, 5, 6, 7, 8, 9, 0, 1])

namedtuple

标准的元组使用数值索引来访问其成员

bob = ('Bob', 30, 'male')print ('Representation:', bob)

jane= ('Jane', 29, 'female')print ('\nField by index:', jane[0])print ('\nFields by index:')for p in[ bob, jane ]:print ('%s is a %d year old %s' % p)

Representation: ('Bob', 30, 'male')

Field by index: Jane

Fields by index:

Bobis a 30year old male

Janeis a 29 year old femal

记住每个索引对应的值是很容易出错的,尤其是在元组有多个元素的情况下。namedtuple为每个成员分配了名字。

importcollections

Person= collections.namedtuple('Person', 'name age gender')print ('Type of Person:', type(Person))

bob= Person(name='Bob', age=30, gender='male')print ('\nRepresentation:', bob)

jane= Person(name='Jane', age=29, gender='female')print ('\nField by name:', jane.name)print ('\nFields by index:')for p in[ bob, jane ]:print ('%s is a %d year old %s' % p)

Type of Person: Representation: Person(name='Bob', age=30, gender='male')

Field by name: Jane

Fields by index:

Bobis a 30year old male

Janeis a 29 year old female

字段名称解析,无效值会导致ValueError

importcollectionstry:

collections.namedtuple('Person', 'name class age gender')exceptValueError, err:print(err)try:

collections.namedtuple('Person', 'name age gender age')exceptValueError, err:print (err)

Type names and field names cannot be a keyword: 'class'Encountered duplicate field name:'age'

OrderedDict

OrderedDict是字典子类,记得其内容被添加的顺序

importcollectionsprint ('Regular dictionary:')

d={}

d['a'] = 'A'd['b'] = 'B'd['c'] = 'C'd['d'] = 'D'd['e'] = 'E'

for k, v ind.items():print( k, v)print ('\nOrderedDict:')

d=collections.OrderedDict()

d['a'] = 'A'd['b'] = 'B'd['c'] = 'C'd['d'] = 'D'd['e'] = 'E'

for k, v ind.items():print (k, v)

Regular dictionary:

a A

c C

b B

e E

d D

OrderedDict:

a A

b B

c C

d D

e E

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值