字典和集合细节
hash('cyberbit')
-5371071624281466360
hash((1,2,3))
2528502973977326415
hash([1,2,3])
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unhashable type: 'list'
这是不合适的!
字典是key-value形式的
key是唯一不可变的,字符串,数字,元组
value是可变的,不唯一的
字典是无序的,查找速度快
info={}
info={'alex':[25,'python','male'],'cyber':['shanghai','mysql']}
info
{'alex': [25, 'python', 'male'], 'cyber': ['shanghai', 'mysql']}
info['alex']
[25, 'python', 'male']
info['cyber']
['shanghai', 'mysql']
info['cyber'][1]='python'
info['cyber']
['shanghai', 'python']
列表有序,是因为有索引,需要索引找到数据
字典无序,是因为没有索引,通过key找到数据,不需要索引
字典key的数据类型,就是hash
字典查找快的原因?
字典存储的是hash,是数字,数字可以比大小。把数字排序很快,然后折半查找也很快。
快到什么程度?
2**20
1048576
最多20次折半,就可以从100万的数据里精确找到值!
字典操作:
增删改查
info['zhangsan']='lisi'
info
{'alex': [25, 'python', 'male'], 'cyber': ['shanghai', 'python'], 'zhangsan': 'lisi'}
info['alex']=99
info
{'alex': 99, 'cyber': ['shanghai', 'python'], 'zhangsan': 'lisi'}
info
{'alex': 99, 'cyber': ['shanghai', 'python'], 'zhangsan': 'lisi'}
'alex' in info
True
info.get('alex')
99
print(info.get('alex2'))
None
info.get('alex2')
info['alex2']
Traceback (most recent call last):
File "<input>", line 1, in <module>
KeyError: 'alex2'
get方法,没有的值是None,【】取的话,没有会报错。
info
{'alex': 99, 'cyber': ['shanghai', 'python'], 'zhangsan': 'lisi'}
info.pop('alex')
99
info
{'cyber': ['shanghai', 'python'], 'zhangsan': 'lisi'}
info.popitem()
('zhangsan', 'lisi')
info
{'cyber': ['shanghai', 'python']}
del info['cyber']
info
{}
多级字典嵌套
dic1 = {
'alex': ['python', 22],
'catalog': {
'av1': ['tits', 'japan', 'G cup'],
'av2': ['ass', 'us', 'I cup']},
'cyber': 'www.cnblogs.com/cyberbit'
}
dic1.keys()
dict_keys(['alex', 'catalog', 'cyber'])
dic1.values()
dict_values([['python', 22], {'av1': ['tits', 'japan', 'G cup'], 'av2': ['ass', 'us', 'I cup']}, 'www.cnblogs.com/cyberbit'])
dic1.items()
dict_items([('alex', ['python', 22]), ('catalog', {'av1': ['tits', 'japan', 'G cup'], 'av2': ['ass', 'us', 'I cup']}), ('cyber', 'www.cnblogs.com/cyberbit')])
dic2 = {'one': 1, 'cyber': 'cnblogs.com/cbyer'}
dic1.update(dic2)
dic1
{'alex': ['python', 22], 'catalog': {'av1': ['tits', 'japan', 'G cup'], 'av2': ['ass', 'us', 'I cup']}, 'cyber': 'cnblogs.com/cbyer', 'one': 1}
字典的update
把没有的扩展进去,类似列表的extend,已有的项“覆盖更新”
dic1
{'alex': ['python', 22], 'catalog': {'av1': ['tits', 'japan', 'G cup'], 'av2': ['ass', 'us', 'I cup']}, 'cyber': 'cnblogs.com/cbyer', 'one': 1}
dic1.setdefault('alex','oldboy')
['python', 22]
dic1
{'alex': ['python', 22], 'catalog': {'av1': ['tits', 'japan', 'G cup'], 'av2': ['ass', 'us', 'I cup']}, 'cyber': 'cnblogs.com/cbyer', 'one': 1}
dic1.setdefault('wu sir','oldboy')
'oldboy'
dic1
{'alex': ['python', 22], 'catalog': {'av1': ['tits', 'japan', 'G cup'], 'av2': ['ass', 'us', 'I cup']}, 'cyber': 'cnblogs.com/cbyer', 'one': 1, 'wu sir': 'oldboy'}
字典的setdefault()是已有的key会返回值,不做改变;没有的key,赋值加入。
dic3={}
dic3=dic3.fromkeys(['a','b','c'])
dic3
{'a': None, 'b': None, 'c': None}
dic3=dic3.fromkeys(['a','b','c'],'22')
dic3
{'a': '22', 'b': '22', 'c': '22'}
fromkeys就是批量生成一个字典
for k in dic1:
print(k,dic1[k])
列表循环,就用这种,高效!
for k,v in dic1.items():
print(k,v)
这种items(),需要转换成列表,低效不用!
_________________________________________________________________________
集合,无序,不重复
取交,并,差集
s={1,2,3,3,2}
s
{1, 2, 3}
s=set([1,2,3,3,2])
s
{1, 2, 3}
s
{1, 2, 3}
s.add(2)
s.add(4)
s
{1, 2, 3, 4}
s.pop()
1
s
{2, 3, 4}
随机删除一个值
s.remove(2)
s
{3, 4}
s
{3, 4}
s.discard(7)
s
{3, 4}
s.discard(3)
s
{4}
删除一个,如果没有:remove报错,discard不报错
s
{4}
s.update(1,2,3,3)
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'int' object is not iterable
s.update({1,2,3,3})
s
{1, 2, 3, 4}
s.update([7,8,7])
s
{1, 2, 3, 4, 7, 8}
s.clear()
清空
s1={1,2,3,4}
s2={3,4,5,6}
s1&s2
{3, 4}
s1-s2
{1, 2}
s2-s1
{5, 6}
s1|s2
{1, 2, 3, 4, 5, 6}
s1^s2
{1, 2, 5, 6}
s1
{1, 2, 3, 4}
s2
{3, 4, 5, 6}
s2.update(s1-s2)
s2
{1, 2, 3, 4, 5, 6}
s1.issubset(s2)
True
s2.issuperset(s1)
True
s1>=s2
False
s2>=s1
True
s1<=s2
True
s2<=s1
False
s1
{1, 2, 3, 4}
s2
{1, 2, 3, 4, 5, 6}
s3={7,8,9}
s1.isdisjoint(s2)
False
s1.isdisjoint(s3)
True
s1
{1, 2, 3, 4}
s2={3,4,5,6}
s1.difference_update(s2)
s1
{1, 2}
把s1-s2的差集赋值给s1
s1={1,2,3}
s2={2,3,4}
s1.intersection_update(s2)
s1
{2, 3}
把s1&s2的并集赋值给s1