The dictionary (Dictionary) get() function in python returns the value of the specified key

There are two ways to read the value of the specified key in the dictionary. The first is the common dict[key] ; the second is to use the dict.get(key) function. details as follows:

1. Function details:

2. The difference between get() method VS dict[key] accessing elements:

3. Nested dictionary usage:

Example:

## 嵌套字典
a = {'path':
        {'corpus_path':'/data/datasets/LibriTTS', 
         'raw_path':'/data/training_data/raw_data/LJSpeech'},
     'preprocessing':
        {'val_size':512,
         'language':'en'}}

## 方法1:dict.get(key)
val_size0 = a.get('preprocessing').get('val_size')
print(val_size0)   ## 512

## 方法2:dict[key]
val_size1 = a['preprocessing']['val_size']
print(val_size1)   ## 512

 Reference: Python dictionary (Dictionary) get() method | rookie tutorial (runoob.com)

 

Guess you like

Origin blog.csdn.net/m0_46483236/article/details/123757802