字典dic内部常用功能和使用方法-课堂笔记及课后总结

查看字典内部方法,使用help(list)、dir(list)来查看。

1、clear:清除字典所有内容

D.clear() -> None. Remove all items from D.

dic1 = {'k1':'v1','k2':'v2'}
dic1.clear()
print(dic1)

#屏幕打印{},dic1已被清空
View Code

2、copy:复制字典内容(浅拷贝)

D.copy() -> a shallow copy of D

dic1 = {'k1':'v1','k2':'v2','k3':'v3'}
dic2 = dic1.copy()
print(dic2)

#屏幕打印{'k1':'v1','k2':'v2','k3':'v3'}
View Code

3、fromkeys:从列表或其他对象来创建字典,S为新字典的keys,v为新字典中的值(可以不赋值)

dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.
v defaults to None.

li1 = ['name','age','sex']
dic1 = dict.fromkeys(li1)
print('New Dic:%s' % (dic1))
dic1 = dict.fromkeys(li1,'10')
print('New Dic:%s' % (dic1))

#屏幕打印:New Dic:{'name': None, 'age': None, 'sex': None}
                New Dic:{'name': '10', 'age': '10', 'sex': '10'}          
View Code

4、get:从字典中get指定的key的值,如果key不存在,则返回设置的默认值,不设置默认值则为None

D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.

dic1 = {'k1':'v1','k2':'v2'}
result = dic1.get('k1','None')
print(result)
result = dic1.get('k3','None')
print(result)

#屏幕打印:v1
                None
View Code

5、has_key:检查字典中是否存在这个key,如果没有则返回False,有则返回True

D.has_key(k) -> True if D has a key k, else False

dic1 = {'k1':'v1','k2':'v2'}
result = 'k1' in dic1
print(result)

#python3中已经无此用法,所以用in取代这种方法
View Code

6、items:将字典中的键值对返回为列表内部为元组,[(键,值),(键,值)]的形式,循环时可以使用 for k,v in dic.items()

D.items() -> list of D's (key, value) pairs, as 2-tuples

dic1 = {'k1':'v1','k2':'v2'}
result = dic1.items()
print(result)

#屏幕打印:dict_items([('k1', 'v1'), ('k2', 'v2')])
View Code

7、iteritems:将字典中所有的键值对返回为一个迭代器

D.iteritems() -> an iterator over the (key, value) items of D

8、iterkeys:将字典中的所有键返回一个迭代器

D.iterkeys() -> an iterator over the keys of D

9、itervalues:将字典中的所有值返回一个迭代器

D.itervalues() -> an iterator over the values of D

#python3.6中已经删除了以上几种用法,故不写代码

10、keys:返回字典中所有的key为一个列表

D.keys() -> list of D's keys

dic1 = {'k1':'v1','k2':'v2'}
result = dic1.keys()
print(result)

#屏幕打印:dict_keys(['k1', 'k2'])
View Code

11、pop:返回字典中指定的键值并移除指定的键的键值对项,如果没有找到指定的键将返回定义的内容,如果没有定义则报错keyError

D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised

dic1 = {'k1':'v1','k2':'v2'}
result = dic1.pop('k1','Error')
print(result)
result = dic1.pop('k3','Error')
print(result)

#屏幕打印:v1
                Error
View Code

12、popitem:返回并删除字典中的最后位置的键值对,如果字典已经为空则报错KeyError

D.popitem() -> (k, v), remove and return some (key, value) pair as a
2-tuple; but raise KeyError if D is empty.

dic1 = {'k1':'v1','k2':'v2','k3':'v3'}
result = dic1.popitem()
print(result)

#屏幕打印:('k3', 'v3')
View Code

13、setdefault:如果字典不存在这个key,则创建,值为None,如果已经存在key,就返回这个key

D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

dic1 = {'k1':'v1','k2':'v2','k3':'v3'}
result = dic1.setdefault('k3')
print(result)
result = dic1.setdefault('k4')
print(result)
print(dic1)

#屏幕打印:v3
                None
                {'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': None}
View Code

14、update:把另一个字典的键值对更新到当前字典中

D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
If E present and has a .keys() method, does: for k in E: D[k] = E[k]
If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
In either case, this is followed by: for k in F: D[k] = F[k]

dic1 = {'k1':'v1','k2':'v2','k3':'v3'}
dic2 = {'k1':'v5'}
dic1.update(dic2)
print(dic1)
dic2 = {'k4':'v4'}
dic1.update(dic2)
print(dic1)

#屏幕打印:{'k1': 'v5', 'k2': 'v2', 'k3': 'v3'}
                {'k1': 'v5', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4'}
View Code

15、values:返回字典中所有的值为一个列表

D.values() -> list of D's values

dic1 = {'k1':'v1','k2':'v2','k3':'v3'}
result = dic1.values()
print(result)

#屏幕打印:dict_values(['v1', 'v2', 'v3'])
View Code

以上为本人课堂笔记,如有不全,欢迎补充!

原文地址:https://www.cnblogs.com/parr2017/p/7793785.html