Python映射类型、集合类型
映射类型:字典
# coding=utf-8
__author__ = 'zyt'
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
print d.keys()
运行结果:
['a', 'c', 'b', 'd']
集合类型:set
- 可变集合set
- 不可变集合frozenset
# coding=utf-8
__author__ = 'zyt'
s1 = {1, 2, 3, 4}
s2 = {3, 4, 5, 6}
print s1 | s2 # 联合union
print s1 & s2 # 交集
print s1 - s2 # 差集
print s1 ^ s2 # 异或,对称差分
# 子集、超集
print set('shop') <= set('bookshop') # set('shop')等价于{'s','h','o','p'}
运行结果:
set([1, 2, 3, 4, 5, 6])
set([3, 4])
set([1, 2])
set([1, 2, 5, 6])
True