05- depth of python dict set and

First, the depth of the set and python dict

1.1, abc inheritance of dict

from collections.abc Import the Mapping, MutableMapping
 # dict mapping type belongs 
a} = {    # dictionary are not inherited a MutableMapping, but realized MutableMapping magic function 
Print (the isinstance (a, MutableMapping)) # True

1.2, dict common method




 print(new_dict) #{'lishuntao': {'company': 'HUT'}, 'lishuntao1': {'company': 'hut1'}}
# print(a) #{'lishuntao': {'company': 'HUT'}, 'lishuntao1': {'company': 'hut1'}}

#深拷贝
import copy
new_dict = copy.deepcopy(a)
new_dict["lishuntao"]["company"] = "HUT"
print(a) #{'lishuntao': {'company': 'hut'}, 'lishuntao1': {'company': 'hut1'}} 
fromkeys iterations will be converted to an object dict#{ 'lishuntao': { 'Company': 'HUT'}, 'lishuntao1': { 'Company': 'hut1'}}#(new_dict)Print


= new_list The [ " Li " , " in Li1 " , " LI2 " ] 
new_dict = dict.fromkeys (new_list The, { " Company " : " Hut " })
 Print (new_dict) # { 'Li': { 'Company': 'Hut '},' in Li1 ': {' Company ':' Hut '},' LI2 ': {' Company ':' Hut '}} 

# GET # If the key does not throw exception 
value = a.get ( " lishuntao111 " ,
{})
print(value) #{}

#items()
for key,value in a.items():
    print(key,value)    #lishuntao {'company': 'hut'},lishuntao1 {'company': 'hut1'}

#setdefault
new_dict = a.setdefault("litao","hugongda")
print(new_dict) #hugongda
print(a) #{'lishuntao': {'company': 'hut'}, 'lishuntao1': {'company': 'hut1'}, 'litao': 'hugongda'}

#update #可迭代的对象
#a.update([("lishun","tao"),("li110","hu")])
a.update(lishun='tao',li110="hu")# Same effect

1.3, dict subclass

# # Do not recommend inherit dict and List 
# class myDict (dict): 
#      DEF __setitem __ (Self, Key, value): 
#          Super (myDict, Self) .__ setitem __ (Key, value * 2) 
# 
# my_dict = myDict (One = . 1) 
# Print (my_dict) {# 'One':. 1} 
# my_dict [ "One"]. 1 = {# 'One': 2} 
# Print (my_dict) 
# "" " 
# , in some cases, by C python language write out the built-in types, it does not call __setitem__ method 
# so we want to inherit dict, then went to inherit UserDict (example below) 
# "" " 
from the Collections Import the UserDict
 # 
# 
# class Mydict1 (the UserDict): 
#     def __setitem__(self, key, value):
#         super(Mydict1, self).__setitem__(key,value*2)
#
# my_dict = Mydict1(one=1)
# print(my_dict) #{'one': 2}
# my_dict["one"] = 1 #{'one': 2}
# print(my_dict)

from collections import defaultdict
"""
defaultdict重写了__missing__方法
"""
my_dict = defaultdict(dict)
my_value = my_dict["lishun"{}=
my_dict1{}#(MY_VALUE)  PrintUnder normal circumstances will report an error#]


my_value1 = my_dict1["lishun"]
print(my_value1) #KeyError: 'lishun'

1.4, set and frozenset

# SET frozenset immutable unordered set, will not be repeated 
S = SET ( " ABCDE " ) 
s.add ( " U " )
 Print (S) # { 'B', 'A', 'E', 'D', 'C', 'U'} 

S2 = frozenset ( " abcdef " ) # frozenset as the dict Key 
Print (S2)   # frozenset ({ 'B', 'F', 'a', 'E', 'D' , 'c'}) # can not be modified 

# -difference 
anoth_set = SET ( " cdefgh " ) 
RES = S.
difference(anoth_set)
print(res)  #{'a', 'b', 'u'}

# Set operation and difference sets post 
RES = S - anoth_set 
RES1 = S & anoth_set # post 
RES2 = S | anoth_set # and 
# high performance set # achieve __contains__ magic function can be done if Analyzing

The principle 1.5, dict and set the

  dict and list find elements, dict lookup performance far greater than the list , the list with the list increased data seek time increases, dict find an element does not increase with the increase in the data, dict implemented principle is called the hash table. Store logic hash table, the hash table is stored in the right side of the structure value, contiguous array, the memory array directivity key and value pointer.

                                                        

 

哈希表的查找:首先计算dict的键的hash值,然后利用hash值定位数组中的一个表元,此时判断表元是否为空,如果表元为空,直接抛出KeyError,如果表元不为空,则判断键是否相等,如果相等的话返回表元里的值,如果不相等的话,则使用hash值的另一部分来定位hash表的另一行,然后又判断表元是否为空进入下一次判断循环。

 特性:

  1、dictkey或者set的值都必须是可以hash的(不可变对象,都是可以hash,str,frozenset,tuple,自己实现的类__hash__都是可以hash的)

  2、dict的内存花销大,但是查询速度快,自定义的对象或者python内部的对象都是用dict包装的

  3、dict的存储顺序和元素添加顺序有关

  4、添加数据有可能改变已有的数据的顺序

Guess you like

Origin www.cnblogs.com/lishuntao/p/11992696.html