Python中Dict与Set

1.dict的赋值、访问、更新、删除

dict是key-value形式的存储结构,value值通过key来赋值、访问、更新、删除等,是一种无序的结构

1)赋值
>>> dict1 = {}
>>> dict1
{}
>>> dict2 = {'name': 'earth', 'port': 80}
>>> dict2
{'name': 'earth', 'port': 80}
>>> dict3 = dict((['x', 1], ['y', 2]))
>>> dict3
{'y': 2, 'x': 1}
>>> dict4 = {}.fromkeys(('x', 'y'), 1)
>>> dict4
{'y': 1, 'x': 1}


2)访问
>>> dict2['name']
'earth'
>>> for key in dict2:
    print 'key = %s, value = %s' % (key, dict2[key])

    
key = name, value = earth
key = port, value = 80
>>> for i in dict2.keys():
    print 'key = %s, value = %s' % (i, dict2[i])

    
key = name, value = earth
key = port, value = 80
>>> for key, value in dict2.items():
    print 'key = %s, value = %s' % (key, value)

    
key = name, value = earth
key = port, value = 80

3)更新
>>> dict2
{'name': 'earth', 'port': 80}
>>> dict2['port'] = 256
>>> dict2
{'name': 'earth', 'port': 256}
>>> dict2['service'] = 'http'
>>> dict2
{'name': 'earth', 'service': 'http', 'port': 256}
>>> dict3
{'y': 2, 'x': 1}
>>> dict2.update(dict3)
>>> dict2
{'y': 2, 'x': 1, 'name': 'earth', 'service': 'http', 'port': 256}

4)删除
>>> dict2
{'y': 2, 'x': 1, 'name': 'earth', 'service': 'http', 'port': 256}
>>> del dict2['x']
>>> dict2
{'y': 2, 'name': 'earth', 'service': 'http', 'port': 256}
>>> dict2
{'y': 2, 'name': 'earth', 'service': 'http', 'port': 256}
>>> dict2.pop('y')
2
>>> dict2
{'name': 'earth', 'service': 'http', 'port': 256}
>>> dict2.clear()
>>> dict2
{}
>>> del dict2
>>> dict2

Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    dict2
NameError: name 'dict2' is not defined

2.dict的比较

比较法:cmp、<、>等
比较顺序:长度---->键---->值

3.映射类型的内建方法

dict.clear()清除dict中的所有元素
dict.fromkeys(seq, val=None)创建并返回一个新dict,以seq中的元素做键,以val值做所有键的初始值
dict.items()返回一个包含dict中key和value元组的列表
dict.keys()返回一个包含dict中key的列表
dict.values()返回一个包含dict中value的列表
dict.pop(key[, default])删除并返回key对应的value,若key不存在,返回default,引发KeyError异常
dict.update(dict2)将dict2中的key-value添加到dict


dict.get(key, default=None)返回key值对应的value值,如不存在key,返回default
>>> dict2
{'name': 'earth', 'service': 'http', 'port': 256}
>>> dict2.get('service')
'http'

dict.has_key(key)若key存在,返回True,若不存在,返回False
>>> dict2.has_key('name')
True
>>> dict2.has_key('x')
False

4.集合

集合对象是一组无序排序的可哈希的值。结合本身是无序的,因此,你不可能为结合创建索引或执行切片操作,也没有键可用来获取集合中元素的值,集合中的元素不可重复

集合分为两种:可变集合set和不可变集合frozenset

1)创建或者赋值:只能使用工厂函数set()或者frozenset()
>>> s = set('Python')
>>> s
set(['h', 'o', 'n', 'P', 't', 'y'])
>>> fs
frozenset(['H', 'e', 'l', 'o'])

2)访问
>>> 'P' in s
True
>>> 's' in s
False
>>> for i in fs:
    print i,

    
H e l o

3)更新
>>> s.add(2)
>>> s
set([2, 'h', 'o', 'n', 'P', 't', 'y'])
>>> s.add('string')
>>> s
set([2, 'string', 'h', 'o', 'n', 'P', 't', 'y'])
>>> s.update('Life')
>>> s
set([2, 'e', 'string', 'f', 'i', 'h', 'L', 'o', 'n', 'P', 't', 'y'])

4)删除
>>> s.remove('P')
>>> s
set([2, 'e', 'string', 'f', 'i', 'h', 'L', 'o', 'n', 't', 'y'])
>>> del s
>>> s

Traceback (most recent call last):
  File "<pyshell#70>", line 1, in <module>
    s
NameError: name 's' is not defined

5.集合类型操作符

1)等价:当且仅当一个集合中的成员同时也是另一个集合中的成员
>>> set1 = set('Python')
>>> set1
set(['h', 'o', 'n', 'P', 't', 'y'])
>>> set2 = set('Hello')
>>> set2
set(['H', 'e', 'l', 'o'])
>>> set3 = set('Python')
>>> set3
set(['h', 'o', 'n', 'P', 't', 'y'])
>>> set1 == set2
False
>>> set1 == set3
True

2)子集与超集:‘<’和‘<=’用来判断子集,‘>’和‘>=’用来判断超集
>>> set('Hello') < set('Hello World!')
True
>>> set('book') >= set('oo')
True

3)并集:‘|’等价于union()方法,两个集合元素的合集
>>> s = set('Python')
>>> b = set('Book')
>>> s
set(['h', 'o', 'n', 'P', 't', 'y'])
>>> b
set(['k', 'B', 'o'])
>>> s | b
set(['P', 'B', 't', 'y', 'h', 'k', 'o', 'n'])

4)交集:‘&’等价于intersection()方法,两个集合中共有的元素,同时属于两个集合
>>> s & b
set(['o'])

5)差集/相对补集:‘-’等价于difference()方法,d = s - b,d中的元素只属于s而不属于b集合的元素
>>> s - b
set(['y', 'h', 't', 'P', 'n'])

6)对称差集:‘^’等价于symmetric_difference()方法,sd = s ^ b,s中的元素只能属于s或者属于b,而不能同时属于两个集合
>>> s ^ b
set(['B', 'h', 'k', 'n', 'P', 't', 'y'])

7)适用于可变类型集合的操作符
|=  &=  -=  ^=
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容