mike.liu

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1.元组

dic.count()

dic.index()

字典

1.fromkeys

dic={'k1'='v1','k2'='v2'}

new_dic=dic.from.fromkeys(['k1','k2','k3'],'v1')

print(new_dic)#返回{'k1':'v1','k2':'v1','k3':'v1'}

2.get

dic={'k1'='v1','k2'='v2'}

print(dic.get('k1'))

print(dic.get('k2'))

print(dic.get('k3'))当没有K3时返回一个none print(dic.get('k3':'lgs'))设置一个默认值 

3.keys values items

dic={'k1'='v1','k2'='v2'}

print(dic.keys()) 返回 dic.keys(['k1','k2'])

print(dic.values())返回 dic.values(['v1','v2'])

print(dic.items())返回 dic.items([('k1','v1'),('k2','v2')])

等价于

for k in dic.keys()

print (k)

for v in dic.values()

print(v)

for k,v in dic.items()

print(k,v)

4.pop

dic.pop(k1)必须指定参数,因为字典是无序的

5.pop.item 随机取走一对key values

dic={'k1'='v1','k2'='v2'}

dic.popitem()

print(dic)

6.update 新增一对值

dic={'k1'='v1','k2'='v2'}

dic.update({'k3':'v3'})

print(dic)

 

posted on 2016-11-07 21:16  mike.liu  阅读(351)  评论(0编辑  收藏  举报