【1.13】字典【2】
def clear(self): # real signature unknown; restored from __doc__
""" D.clear() -> None. Remove all items from D. """
pass
1、清除字典
info = {0:1113,1:122223,"true":"bool",True:1,False:0,0:123,1:123}
print info
info.clear()
print info
# 结果就为空 {}
def copy(self): # real signature unknown; restored from __doc__
""" D.copy() -> a shallow copy of D """
pass
2 、复制字典给新的字典
info = {0: 1113, 1: 122223, "true": "bool", True: 1, False: 0, 0: 123, 1: 123}
info1 = info.copy()
print info
print info1
@staticmethod # known case
def fromkeys(S, v=None): # real signature unknown; restored from __doc__
"""
dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.
v defaults to None.
"""
pass
3、新建 字典 按 fromkeys 方式
#新建字典, 以a元组中的值为键
a=("1",True,False,"abcd","name","age")
info=dict.fromkeys(a)
#这里的值就为空 None
print info
# {'abcd': None, True: None, 'name': None, False: None, 'age': None, '1': None}
info1 = dict.fromkeys(a,"abc")
#这里的键值对,建立就是 a 为键,“abc”为值
print info1
# {'abcd': 'abc', True: 'abc', 'name': 'abc', False: 'abc', 'age': 'abc', '1': 'abc'}
def get(self, k, d=None): # real signature unknown; restored from __doc__
""" D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
pass
4、#获取键对应的值, 如果这个键不存在,那么就返回 get中给定的默认的值 ,如果没有给定,就返回None
info = {0: 1113, 1: 122223, "true": "bool", True: 1, False: 0, 0: 123, 1: 123}
value = info.get(0)
#如果存在这个key 就会返回这个key 对应的值
print value
# 结果:123 或者 1113 或者 0
value = info.get("a1","YAOMING")
print value
# YAOMING
value = info.get("b1")
#键不存在 ,没有给定默认值,就返回None
print value
# None
def has_key(self, k): # real signature unknown; restored from __doc__
""" D.has_key(k) -> True if D has a key k, else False """
return False
5、对key 进行hash 并返回真假,判断是否在字典, 在就返回真,不在就返回假
info = {0: 1113, 1: 122223, "true": "bool", True: 1, False: 0, 0: 123, 1: 123}
#对key hash,判断key 是否在这个字典里。 如不在就返回False 如果不在 就返回True
#当然这里的key 是合法, 不合法的 如可变的元素就不能做为key 如list
a=info.has_key(0)
print a
# True
def items(self): # real signature unknown; restored from __doc__
""" D.items() -> list of D's (key, value) pairs, as 2-tuples """
return []
6、取字典中的键值对
info = {0: 1113, 1: 122223, "true": "bool", True: 1, False: 0, 0: 123, 1: 123}
a=info.items()
print a
#取值的结果就是将 字典中的键值对取出来形成元组,然后再放在列表中
# [(0, 123), (1, 123), ('true', 'bool')]
def iteritems(self): # real signature unknown; restored from __doc__
""" D.iteritems() -> an iterator over the (key, value) items of D """
pass
7、info = {0: 1113, 1: 122223, "true": "bool", True: 1, False: 0, 0: 123, 1: 123}
a= info.iteritems()
print a
# 与items 不一样 ,items取出来就是列表
# 而这个取出来是放在内存 ,你要用的时,就再用 list 取出来
print type(a)
print list(a)
# <dictionary-itemiterator object at 0x0000000002511A98>
# <type 'dictionary-itemiterator'>
# [(0, 123), (1, 123), ('true', 'bool')]
iteritems()方法在需要迭代结果的时候使用最适合,而且它的工作效率非常的高。
总结:
(1)在Python2.x中,iteritems() 用于返回本身字典列表操作后的迭代器【Returns an iterator on all items(key/value pairs) 】,不占用额外的内存。
(2)在Python 3.x 里面,iteritems()方法已经废除了。在3.x里用 items()替换iteritems() ,可以用于 for 来循环遍历。
def iterkeys(self): # real signature unknown; restored from __doc__
""" D.iterkeys() -> an iterator over the keys of D """
pass
8、这里取出来一样是一个地址,就是将key取出来 和 keys不一样
keys 就是取出来就是列表的形式,但是 itemkeys 就是用的时间才用list
def itervalues(self): # real signature unknown; restored from __doc__
""" D.itervalues() -> an iterator over the values of D """
pass
9、同上
def keys(self): # real signature unknown; restored from __doc__
""" D.keys() -> list of D's keys """
return []
10、同上
def pop(self, k, d=None): # real signature unknown; restored from __doc__
"""
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
"""
pass
11、删除一个键对应得键值对,返回该键对应的值,原来的字典中删除该键值对。
若果没有该键,那么就会报错该键不存在
若果pop中自己有默认值,那么没有该键,则返回默认值
info = {0: 1113, 1: 122223, "true": "bool", True: 1, False: 0, 0: 123, 1: 123}
print info
a = info.pop(True,"abc")
print a
# a 的结果就是 123 或者122223 或者1
print info
# {0: 123, 1: 123, 'true': 'bool'}
# 123
# {0: 123, 'true': 'bool'}
def popitem(self): # real signature unknown; restored from __doc__
"""
D.popitem() -> (k, v), remove and return some (key, value) pair as a
2-tuple; but raise KeyError if D is empty.
"""
pass
12、pop返回时键的对应值
而这个 popitem 返回是键对应的键值对
{0: 123, 1: 123, 'true': 'bool'}
(0, 123)
{1: 123, 'true': 'bool'}
def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
""" D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
pass
13、单个扩展
info = {0: 1113, 1: 122223, "true": "bool", True: 1, False: 0, 0: 123, 1: 123}
print info
new_info = info.setdefault("abc","name")
#setdefault 返回的时设置的key,
#新的字典就是添加了新的键值对,如果添加键值对的时,没有添加值的话,默认添加None
print info
print new_info
# {0: 123, 1: 123, 'true': 'bool'}
# {0: 123, 1: 123, 'abc': 'name', 'true': 'bool'}
# name
def update(self, E=None, **F): # known special case of dict.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]
"""
pass
14、批量扩展字典
D = {'one': 1, 'two': 2}
D.update({'three': 3, 'four': 4}) # 传一个字典
print(D)
D.update(five=5, six=6) # 传关键字
print(D)
D.update([('seven', 7), ('eight', 8)]) # 传一个包含一个或多个元祖的列表
print(D)
D.update(zip(['eleven', 'twelve'], [11, 12])) # 传一个zip()函数
print(D)
D.update(one=111, two=222) # 使用以上任意方法修改存在的键对应的值
print(D)
# 以上实例输出结果为:
# {'one': 1, 'three': 3, 'two': 2, 'four': 4}
# {'one': 1, 'four': 4, 'six': 6, 'two': 2, 'five': 5, 'three': 3}
# {'one': 1, 'eight': 8, 'seven': 7, 'four': 4, 'six': 6, 'two': 2, 'five': 5, 'three': 3}
# {'one': 1, 'eight': 8, 'seven': 7, 'four': 4, 'eleven': 11, 'six': 6, 'twelve': 12, 'two': 2, 'five': 5, 'three': 3}
# {'four': 4, 'seven': 7, 'twelve': 12, 'six': 6, 'eleven': 11, 'three': 3, 'one': 111, 'eight': 8, 'two': 222, 'five': 5}
def values(self): # real signature unknown; restored from __doc__
""" D.values() -> list of D's values """
return []
15、同key,返回列表
def viewitems(self): # real signature unknown; restored from __doc__
""" D.viewitems() -> a set-like object providing a view on D's items """
pass
def viewkeys(self): # real signature unknown; restored from __doc__
""" D.viewkeys() -> a set-like object providing a view on D's keys """
pass
def viewvalues(self): # real signature unknown; restored from __doc__
""" D.viewvalues() -> an object providing a view on D's values """
pass
16.17.18、用的比较少 py3 不用
info = {0: 1113, 1: 122223, "true": "bool", True: 1, False: 0, 0: 123, 1: 123}
a = info.viewkeys()
b = info.viewitems()
c = info.viewvalues()
print a
print b
print c
print info
# dict_keys([0, 1, 'true'])
# dict_items([(0, 123), (1, 123), ('true', 'bool')])
# dict_values([123, 123, 'bool'])
# {0: 123, 1: 123, 'true': 'bool'}
py3里面的字典功能:
info.clear( info.get( info.pop( info.update(
info.copy( info.items( info.popitem( info.values(
info.fromkeys( info.keys( info.setdefault(
浙公网安备 33010602011771号