每天十分钟,十二天入门Python(十二)

collections集合模块

from collections import namedtuple


Point = namedtuple('Point',['x','y'])

p = Point(1,2)

print p.x

print p.y

p.z = 3

# 1

# 2

# Traceback (most recent call last):

#   File "/Users/apple/workspace/pyDemo/demo.py", line 11, in <module>

#     p.z = 3

# AttributeError: 'Point' object has no attribute 'z'


namedtuple()实际上就是创建了一个tuple对象,但是可以根据属性直接引用,属性值和属性个数都不可变,和tuple的不变性一致.

from collections import deque


arr = deque([1,2,3,4,5])

arr.append(6)

arr.appendleft(0)

print arr

# deque([0, 1, 2, 3, 4, 5, 6])


deque()除了实现list的append()和pop()外,还支持appendleft()和popleft(),这样就可以非常高效地往头部添加或删除元素.

from collections import defaultdict


dd = defaultdict(lambda: 'N/A')

dd['key1'] = 'abc'

print dd['key1']     # key1存在

# 'abc'

print dd['key2']     # key2不存在,返回默认值

# 'N/A'


defaultdict()相比于dict的进步就是在获取不存在的key的时候不会报错~

from collections import OrderedDict


d = dict([('name','zhangsan'),('age',22)])

print d

d2 = OrderedDict([('name','zhangsan'),('age',22)])

print d2

# {'age': 22, 'name': 'zhangsan'}

# OrderedDict([('name', 'zhangsan'), ('age', 22)])

o = OrderedDict()

o['x'] = 1

o['z'] = 3

o['y'] = 2

print o.keys()

print o

# ['x', 'z', 'y']

# OrderedDict([('x', 1), ('z', 3), ('y', 2)])



OrderDict()创建了有序的dict,其中key的顺序和插入的顺序一致.

from collections import Counter


c = Counter()

for char in 'hello world':

    c[char] = c[char]+1

print c

# Counter({'l': 3, 'o': 2, ' ': 1, 'e': 1, 'd': 1, 'h': 1, 'r': 1, 'w': 1})


Counter()用来计数,比如上面的demo记录了每个字母出现的次数.

itertools迭代模块

import itertools


ns = itertools.count()

for n in ns:

    print n

# 1

# 2

# 3

# ...

# count()会创建一个无限迭代器




ns = itertools.cycle([1,2,3])

for n in ns:

    print n

# 1

# 2

# 3

# 1

# 2

# 3

# ...

# cycle会无限的重复指定的序列

ns = itertools.repeat([1,2,3],2)

for n in ns:

    print n

# [1, 2, 3]

# [1, 2, 3]

# repeat的内容会重复循环,第二个参数可以设定循环次数




for n in itertools.chain('QWE','RTY','U'):

    print n

# Q

# W

# E

# R

# T

# Y

# U

# chain()可以将可迭代对象串起来,形成更大的迭代器



for key,group in itertools.groupby('AADDWWCCVVCCEEAA'):

    print key,list(group)

print '--------------------------------------------------'

for key2,group2 in itertools.groupby('AADDWWCCVVCCEEAAaaccswww',lambda c:c.upper()):

    print key2,list(group2)

    

# A ['A', 'A']

# D ['D', 'D']

# W ['W', 'W']

# C ['C', 'C']

# V ['V', 'V']

# C ['C', 'C']

# E ['E', 'E']

# A ['A', 'A']

# --------------------------------------------------

# A ['A', 'A']

# D ['D', 'D']

# W ['W', 'W']

# C ['C', 'C']

# V ['V', 'V']

# C ['C', 'C']

# E ['E', 'E']

# A ['A', 'A', 'a', 'a']

# C ['c', 'c']

# S ['s']

# W ['w', 'w', 'w']

# 一般情况下groupby()仅能将相邻重复的元素挑出来,实际上挑选规则是通过函数完成的,只要作用于函数的两个元素返回的值相等,这两个元素就被认为是在一组的,而函数返回值作为组的key。如果我们要忽略大小写分组,就可以让元素'A'和'a'都返回相同的key

你可能感兴趣的:(每天十分钟,十二天入门Python(十二))