python 字典

无序,通过键值对来存储数据

可嵌套。异构,映射操作

散列表

基本操作

d = {'spam':2,'ham':3,'eggs':4}
print(d['spam']) # 2
print(len(d)) # 3
print('ham' in d) # True
print(list(d.keys())) # ['spam', 'ham', 'eggs']

原处修改

d = {'spam':2,'ham':3,'eggs':4}
d['ham'] = [1,2,4]
print(d) # {'spam': 2, 'ham': [1, 2, 4], 'eggs': 4}
del d['ham']
print(d) # {'spam': 2, 'eggs': 4}
d['spam'] = 'lux'
print(d) # {'spam': 'lux', 'eggs': 4}
d = {'spam':2,'ham':3,'eggs':4}
print(list(d.items())) # [('spam', 2), ('ham', 3), ('eggs', 4)]
print(list(d.keys())) # ['spam', 'ham', 'eggs']
print(list(d.values())) # [2, 3, 4]
print(d.get('eee','error')) # error
print(d.get('spam','error')) # 2
print(d.get('sdf')) # None
d = {'spam':2,'ham':3,'eggs':4}
e = {'jinx':5}
d.update(e)
print(d) # {'spam': 2, 'ham': 3, 'eggs': 4, 'jinx': 5}
print(d.pop('jinx')) # 5
print(d) # {'spam': 2, 'ham': 3, 'eggs': 4}
table = {'python':'Gudio van Rossum',
'Perl': 'Larry Wall',
'Tcl': 'John Qusterhout'}
language = 'python'
creator = table[language]
print(creator) # Gudio van Rossum
for i in table:
print(i,'\t',table[i])

python Gudio van Rossum

Perl Larry Wall

Tcl John Qusterhout

注意

序列运算无效

对新索引赋值会添加项

键不一定总是字符串

稀疏结构

dic = {}
dic[(2,3,4)] = 88
dic[(7,8,9)] = 99
x = 2;y = 3;z = 4
print(dic[(x,y,z)]) # 88
print(dic) # {(2, 3, 4): 88, (7, 8, 9): 99}
if (2,3,5) in dic:
print(dic[(2,3,5)])
else:
print(0) # 0

try:
print(dic[(2,3,5)])
except KeyError:
print(0) # 0
print(dic.get((2,3,4),0)) # 88
print(dic.get((2,3,5),0)) # 0

创建字典的其他方法

print({'spam':2,'ham':3}) # {'spam': 2, 'ham': 3}
d = {}
d['spam'] = 2
d['ham'] = 3
print(d) # {'spam': 2, 'ham': 3}
print(dict(spam = 2,ham =3)) # {'spam': 2, 'ham': 3}
print(dict([('spam',2),('ham',3)])) # {'spam': 2, 'ham': 3}
print(dict.fromkeys([1,2],0)) # {1: 0, 2: 0}
print(dict.fromkeys(['spam','ham'],1)) #{'spam': 1, 'ham': 1}

字典解析

print(list(zip((1,2,4),(3,4,5)))) # [(1, 3), (2, 4), (4, 5)]
print(dict(zip((1,2,4),(3,4,5)))) # {1: 3, 2: 4, 4: 5}
print({k:v for (k,v) in zip([1,2,3],[4,5,6])}) # {1: 4, 2: 5, 3: 6}
print({x:x2 for x in [1,2,3,4]}) # {1: 2, 2: 4, 3: 6, 4: 8}
print({c:c
4 for c in 'spam'})#{'s': 'ssss', 'p': 'pppp', 'a': 'aaaa', 'm': 'mmmm'}
d={c.lower():c+'!' for c in 'spam'}
print(d) # {'s': 's!', 'p': 'p!', 'a': 'a!', 'm': 'm!'}
print({k:0 for k in ['a','b','c']}) # {'a': 0, 'b': 0, 'c': 0}
print(dict.fromkeys('spam')) # {'s': None, 'p': None, 'a': None, 'm': None}
print({s:None for s in 'spam'}) # {'s': None, 'p': None, 'a': None, 'm': None}

字典视图和几何

d = {'a':1,'b':2,'c':3}
k = d.keys()
v = d.values()
print(k | {'x':4}) # {'c', 'b', 'a', 'x'}
d = {'a':1,'b':2,'c':3}
print(d.keys()&d.keys()) # {'c', 'b', 'a'}
print(d.keys()&{'b'}) # {'b'}
print(d.keys()&{'b':1}) # {'b'}
print(d.keys()|{'b','c','d'}) # {'d', 'a', 'c', 'b'}
d = {'a':1}
print(list(d.items())) # [('a', 1)]
print(d.items()|d.keys()) # {('a', 1), 'a'}
print(d.items()|d) # {('a', 1), 'a'}
print(d.items()|{('c',3),('d',4)}) # {('c', 3), ('d', 4), ('a', 1)}
print(dict(d.items()|{('c',3),('d',4)})) # {'a': 1, 'c': 3, 'd': 4}
d = {'a':1,'b':2,'c':3}
for k

posted on 2019-07-10 08:46  幻嘤剑舞  阅读(86)  评论(0编辑  收藏  举报

导航