Detailed explanation of Python dictionary get() function, dictionary get value

"Author's Homepage": Shibie Sanri wyx
"Author's Profile": CSDN top100, Alibaba Cloud Blog Expert, Huawei Cloud Share Expert, Network Security High-quality Creator
"Recommended Column": Xiaobai Zero Basic "Python Beginner to Master"

get() can "get the value" according to the key

grammar

dict.get( key, value )

parameter

  • key : (required) specifies the key to search for
  • value : (optional) If the key does not exist, return the specified content

return value

  • If the key exists, return the value corresponding to the key
  • Returns the specified content or None if the key does not exist

Example: get the value of the dictionary according to the key

dict1 = {
    
    'key1': 'value1', 'key2': 'value2'}
print(dict1.get('key1'))

output:

value1

1. Set the default return value

get() can specify the "default" " return value" . When the key does not exist, it will not report an error, but return a default value. The advantage of this is that the program will not be abnormal when it is running.

"Not specified" return value, when the key does not exist, it returns None by default

dict1 = {
    
    'key1': 'value1', 'key2': 'value2'}
print(dict1.get('key3'))
print(dict1.get('key3', None))

output:

None
None

"Specify" return value, when the key does not exist, return the specified return value

dict1 = {
    
    'key1': 'value1', 'key2': 'value2'}
print(dict1.get('key3', '指定返回值'))

output:

指定返回值

The return value can be various data types such as integer, string, tuple, list, etc.

dict1 = {
    
    'key1': 'value1', 'key2': 'value2'}
print(dict1.get('key3', 1))
print(dict1.get('key3', 1.1))
print(dict1.get('key3', True))
print(dict1.get('key3', [1, 2]))
print(dict1.get('key3', (1, 2)))
print(dict1.get('key3', {
    
    1, 2}))

output:

1
1.1
True
[1, 2]
(1, 2)
{
    
    1, 2}

2. Nested dictionary value

When "nesting" dictionaries in dictionaries , you can call get() multiple times to get values.

dict1 = {
    
    'key1': 'value1', 'key2': {
    
    'key3': 'value3'}}
print(dict1.get('key2').get('key3'))

# 等价于这样
result = dict1.get('key2')
print(result)
result1 = result.get('key3')
print(result1)

output:

value3
{
    
    'key3': 'value3'}
value3

3. The difference between get() and dict[key]

Both get() and dict[key] can get the value of the dictionary according to the key, the difference is

When the key of get() does not exist in the dictionary, it will return "None" or "specified content"

dict1 = {
    
    'key1': 'value1', 'key2': 'value2'}
print(dict1.get('key3'))
print(dict1.get('key3', '指定内容'))

output:

None
指定内容

When the key of dict[key] does not exist in the dictionary, it will "report an error" KeyError: 'key3'

dict1 = {
    
    'key1': 'value1', 'key2': 'value2'}
print(dict1['key3'])

output:

insert image description here


4. Statistical element counting

get() can "count" the "number" of occurrences of list elements , and save the statistical results in the dictionary

dict1 = {
    
    }
list1 = ['¥', '¥', '¥', '$', '$', '$', '$', '$']
for i in list1:
    dict1[i] = dict1.get(i, 0) + 1

print(dict1)

output:

{
    
    '¥': 3, '$': 5}

Guess you like

Origin blog.csdn.net/wangyuxiang946/article/details/131677257