<<返回python首页 python

《Python 应用案例》

Python标准库: 高级容器数据结构 collections

Python中除包含了丰富的数据类型,如list、dict、和tuple。但在实现使用中之外的容器数据类型,如counter、defaultdict、deque、namedtuple、orderdict,下面将一一介绍。

Python标准库: 高级容器数据结构 collections

Counter

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

import collections

print (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容器可以无参数构造,并采用update()方法进行更新

import collections

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

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

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

访问计数:

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

import collections

c = collections.Counter('abcdaab')

for letter in 'abcde':
    print ('%s : %d' % (letter, c[letter]))

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

import collections

c = collections.Counter('extremely')
c['z'] = 0
print(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个最多的数据

import collections

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

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

import collections

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允许调用者在初始化时预先设置默认值。

import collections

def default_factory():
    return 'default value'

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

OrderedDict

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

import collections

print ('Regular dictionary:')
d = {}
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'
d['d'] = 'D'
d['e'] = 'E'

for k, v in d.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 in d.items():
    print (k, v)

Deque

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

import collections

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)

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

import collections

# 右端插入
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)

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

import collections

print ('From the right:')
d = collections.deque('abcdefg')
while True:
    try:
        print (d.pop())
    except IndexError:
        break

print ('\nFrom the left:')
d = collections.deque('abcdefg')
while True:
    try:
        print (d.popleft())
    except IndexError:
        break

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

import collections
import threading
import time

candle = collections.deque(xrange(11))

def burn(direction, nextSource):
    while True:
        try:
            next = nextSource()
        except IndexError:
            break
        else:
            print ('%8s: %s' % (direction, next))
            time.sleep(0.1)
    print ('%8s done' % direction)
    return

left = 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()

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

import collections

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)

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)

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

import collections

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)

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

import collections

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

try:
    collections.namedtuple('Person', 'name age gender age')
except ValueError, err:
    print (err)
import collections

print ('Regular dictionary:')
d = {}
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'
d['d'] = 'D'
d['e'] = 'E'

for k, v in d.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 in d.items():
    print (k, v)
移动端设备除iPad Pro外,其它移动设备仅能阅读基础的文本文字。
建议使用PC或笔记本电脑,浏览器使用Chrome或FireFox进行浏览,以开启左侧互动实验区来提升学习效率,推荐使用的分辨率为1920x1080或更高。
我们坚信最好的学习是参与其中这一理念,并致力成为中文互联网上体验更好的学练一体的IT技术学习交流平台。
您可加QQ群:575806994,一起学习交流技术,反馈网站使用中遇到问题。
内容、课程、广告等相关合作请扫描右侧二维码添加好友。

狐狸教程 Copyright 2021

进入全屏