#字典是一种映射,
eng2sp = dict()
print(eng2sp)
eng2sp["one"]= 'undo'
print(eng2sp)
eng2sp['two'] = 'df'
print(eng2sp)
d=eng2sp['one']
print(d)
###########字典中的顺序是不可预料的
a=len(eng2sp)#显示键值对的个数
print(a)
#####判定一个值是不是字典中的键
if 'one' in eng2sp:
print(True)
'undo'in eng2sp
##########使用values方法,返回一个值的集合
vals = eng2sp.values()
print(vals)
########使用字典作为计数器的集合
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] +=1
return(d)
s = "skdfdshfkshdfhsdjgajdhfkhdskjf"
js = histogram(s)
print(js)
######get方法,如果键出现在字典中,get返回对应值,否则返回默认值
h = histogram('a')
c = h.get('a',0)
print(c)
d = h.get('b',0)
print(d)
########
def print_hist(h):
for c in h:
print(c,h[c],end=" ")
h = histogram("parrot")
print(print_hist(h))
python3中的字典
最新推荐文章于 2020-08-28 16:02:52 发布