python dictionary (BIF built-in method)

Dictionary is python's only mapping type ------(key,value), that is, it consists of multiple keys and corresponding values

Original, find logon by brand

def index1():
    brand = ['123','345','567','789']
    logon = ['Ha1','Ha2','Ha3','Ha4']
  print("567的logon:",logon[brand.index('567')])

Create a dictionary, use the dict factory function to create

dic1 = {123: 'Ha1', '345': 'Ha12', '567': 'Ha123', '789': 'Ha1234'}
 dict2={} #Create empty dictionary
 dict3 =dict((('1',12), ('2',123), ('3',1234), ('4',12345)))
 dict4 = dict({123:'hhhh','345':'Flying','567':'fine enough','789':'asdff'})
 dict5 = dict(aaaaa='111', bbbbb='2222')
dict5['aaaaa'] = 'Modified,,,,,' #Modify the dictionary
print(dict5) #{'bbbbb': '2222', 'aaaaa': 'Modified,,,,,'}
dict5['ccccc'] = '33333' #Add dictionary
print(dict5) #{'bbbbb': '2222', 'aaaaa': 'Modified,,,,,', 'ccccc': '33333'}

Access by key value: dict5['aaaaa'] #Modified,,,,,


Access dictionary  keys(), values(), items()

def fun1():
    dect1 = {}
    print(dect1.fromkeys((1,2,3),'haha'))
    # {1: 'haha', 2: 'haha', 3: 'haha'}

    dect1 = dect1.fromkeys(range(5),'赞')
    print(dect1)
    #{0: 'like', 1: 'like', 2: 'like', 3: 'like', 4: 'like'}

    for item in dect1.items():
        print(item)
        # (0, 'Like')
        # (1, 'Like')
        # (2, 'like')
        # (3, 'Like')
        # (4, 'Like')


    for each in dect1.keys():
        print(each)


    for value in dect1.values():
         print(value)

When the obtained key is not in the list, a default value can be given:

For example, there is no data with key 10 in the list

print(dect1.get(10))     #None
 print(dect1.get(10),'No....') #No....


Membership operators: in, not in

Python has a membership operator that checks if a value is in a sequence/list/string/... and returns True if it is in the sequence, otherwise returns False

Such as:

4 in dict5 #True (data with key 4 exists in the dict5 dictionary)
31 in dict5 #False (does not exist)
In the dictionary: looking up is the key, not the value
In sequence: looking for value, not key 


Clear dictionary: clear()

dect1.clear()

It is not recommended to use the dect1={} method to clear, examples are as follows:

    dect2 = dect1
    dect1 = {}
    print(dect1)        #{}
    print(dect2) #{0: 'Like', 1: 'Like', 2: 'Like', 3: 'Like', 4: 'Like'}
Here dect1 and dect2 point to the same dictionary address, which are two independent individuals. After dect1 is cleared, dect2 still exists.

Dictionary copy: copy()

copy() only copies the surface data, not the address of the original data
def fun2():
    a = {1: 'one', 2: 'two', 3: 'three'}
    b = a
    c = a.copy()
    print(id(a))        #10255176
    print(id(b))        #10255176
    print(id(c))        #10255624
    b[4] = 'four'
    print(a)            #{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
    print(b)            #{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
    print(c)            #{1: 'one', 2: 'two', 3: 'three'}

Dictionary data deletion: pop(), popitem()

pop(): delete the data of the specified key

popitem() : delete a data randomly (originally delete the last data, but because the dictionary data does not have a certain order, so random)

def fun3():
    a = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
    print(a.pop(2)) #Delete data with key 2: two
    print(a)                # {1: 'one', 3: 'three', 4: 'four'}  
    print(a.popitem()) # Randomly pop a piece of data: (1, 'one')
    print(a)                # {3: 'three', 4: 'four'}

Insert data into dictionary: setdefault()

a.setdefault(5,'five')          #{3: 'three', 4: 'four', 5: 'five'}
b = {'Xiaobai':'dog'}
a.update(b)          #{'小白': 'dog', 3: 'three', 4: 'four', 5: 'five'}    






Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326718012&siteId=291194637