Article From:https://www.cnblogs.com/brightyuxl/p/9123755.html

Python data type (Dictionary)”

dictionary features

  1. Features
    Variable, unordered, mapping, and key value pairs
  2. form
    {key0:value0, key1:value1, key2:value3, …, }

key:Uniqueness, the data type of key must be fixed and immutable, such as numbers, strings, tuples, frozen sets.

value:Arbitrary Python data type

create a dictionary container object

my_study = {'Mathematics':'math is a kind of art',' language ':'writing', 'foreign language':'communication'}Print (my_study){'Math':'math is aKind of art',' language ':'writing', 'foreign language':'communication'}My_character = {1:'beauty', 2:'handsome', 3:'happy', 4:'good'}Print (my_character){1:'beauty', 2:'handsome', 3:'happy', 4:'good'}My_test= {(1,2): 12, (3,4): 34, (4,5): 56}Print (my_test){(1, 2): 12, (3, 4): 34, (4, 5): 56}My_test_temp ={'a': 66, 1:'1', (0,0): [1,2,3], ('a','b'): {'1': 1,'2': 2}}Print (my_test_temp){'a': 66, 1:'1', (0, 0)): [1, 2, 3], ('a','b'): {'1': 1,'2': 2}}My_good_dict = {frozenset ({1,2,3}): 123, frozenset ({4,5).6}): 456}Print (my_good_dict){frozenset ({1, 2, 3}): 123, frozenset ({4, 5, 6}): 456}

Dictionary ‘common operations

  • Adding an element to a dictionary
I_dict = {1:'1', 2:'2', 3:'3'}
I_dict[4] = '4'  # Add the 4:'4'to the key value pairPrint (I_dict){1:'1', 2:'2', 3:'3', 4:'4'}Add'My_ to I_dict.setdefault ('My_add')Add': the key value pair of NonePrint (I_dict){1:'1', 2:'2', 3:'3', 4:'4','My_add': None}I_dict.setdefault (5, '5') add the key value pair of 5:'5' and return to'5'Print (I_dict){1:'1', 2:'2', 3:'3', 4:'4','My_add': None, 5:'5'}I_Dict_1 = {'a': 1,'b': 2,'c': 3, 1:'a'}I_dict.update (I_dict_1) I_dict_1 updates the current dictionary with I_dict_1, and the value of the update is replaced by the updated value.I_dicT{1:'a',2:'2',3:'3',4:'4','My_add': None,5:'5','a': 1,'b': 2,'c': 3}
  • Lookup dictionary elements
dict_temp = {'1':1,'2':2,'3':3,'4':4,'a':[1,2,3],'b':(1,2,3)}

print(dict_temp['a'])
[1, 2, 3]

print(dict_temp.get('2'))
2

print(dict_temp.keys())
dict_keys(['1', '2', '3', '4', 'a', 'b'])

print(dict_temp.values())
dict_values([1, 2, 3, 4, [1, 2, 3], (1, 2, 3)])

print(dict_temp.items())
dict_items([('1', 1), ('2', 2), ('3', 3), ('4', 4), ('a', [1, 2, 3]), ('b', (1, 2, 3))])
  • Element deletion in a dictionary
keys = list(range(4))
values = [x for x in 'abcd']
dict_ = dict(zip(keys, values))
dict_
{0: 'a', 1: 'b', 2: 'c', 3: 'd'}

dict_.pop(2) #Delete the specified key and return the corresponding value. If the key is not found in the dictionary, the KeyError error is displayed.'c'Dict_{0:'a', 1:'b', 3:'d'}Print and delete key values of dict_.popitem ()To return (in the form of tuples), randomly return and delete a pair of keys and values in the dictionary (generally delete the last pair).(3,'d')Dict_Out[32]: {0:'a', 1:'b'}Dict_.clear () dictionariesDict_{}Delete the dictionary directly from del dict_Dict_Traceback (most recent call last):File "< ipython-input-37-247f529b331d> ", line 1, in < module>Dict_NameError: name'dict_'is not defined
  • Modifying the elements in the dictionary
dictionary = dict(zip(list(range(5)),[str(x) for x in range(5)])) #Create a dictionaryDictionary{0:'0', 1:'1', 2:'2', 3:'3', 4:'4'}Print (2 in dictionary) check whether Jian 2 is dictionary.inTrueDictionary[2] ='xiugai''2' modifies the value'2'of kin 2 to'xiugai'Print (Dictionary){0:'0', 1:'1', 2:'xiuGai', 3:'3', 4:'4'}
  • fromkeys()An example
print(dictionary)
{0: '0', 1: '1', 2: 'xiugai', 3: '3', 4: '4'}

x = dictionary.fromkeys([1,2,3],'xyz')   # fromkeys() Declare a new dictionary in the original dictionaryX{1:'xyz', 2:'xyz', 3:'xyz'}Y = {}.fromkeys (['a','b','c'], ['a','b','c'])Y{''A': ['a','b','c'],'b': ['a','b','c'],'c': ['a','b', ['a'
  • Cyclic iteration of dictionaries
x={'a': 1, 'b': 2, 'c': 3, 1: 'a'}

for i in x:
    print(i)
    
a
b
c
1

for i in x:
    print(i, x[i])
    
a 1
b 2
c 3
1 a

for i, j in x.items():
    print(i, j)
    
a 1
b 2
c 3
1 a

Similar Posts:

Link of this Article: The dictionary of the Python3 base

Leave a Reply

Your email address will not be published. Required fields are marked *