📜  如何将 dict.get() 与多维字典一起使用?

📅  最后修改于: 2022-05-13 01:55:50.651000             🧑  作者: Mango

如何将 dict.get() 与多维字典一起使用?

如果键在字典中, Python中的 get() 方法会返回键的值。如果键不存在于字典中,则返回 None。它还接受另一个可选参数,如果在字典中找不到键,则将返回该值。

在这里,键是一个必须参数,它是我们想要从字典中获取其值的键。 value 是一个可选字段,它是在字典中找不到指定键时返回的值。值的默认值为无。

示例 1:

Python3
# single dimension dictionary
d = {'jhon': 22, 'sanie': 34, 'munk': 19}
  
# return value if found if not then return Not found
print(d.get('sanie', 'Not found'))


Python3
# Example of dict.get() with multidimensional dict
dict = {'India': {'captain': 'Virat', 'Batsman': 'Rohit', 
                  'Bolwer': 'Bumrah'},
        'England': {'captain': 'Root', 'Batsman': 'Buttler',
                    'Bolwer': 'anderson'},
        'Australia': {'captain': 'Paine', 'Batsman': 'Warner',
                      'Bolwer': 'Starck'},
        'Pakistan': {'captain': 'Babar', 'Batsman': 'Hafiz',
                     'Bolwer': 'Aamir'},
        'West Indies': {'captain': 'Pollard', 'Batsman': 'Gayle',
                        'Bolwer': 'Narayan'}
        }
  
# find batsman for india
# return Not Found if not exists in dict
print(dict.get('India').get('Batsman', 'Not Found'))


Python3
# Example of dict.get() with multidimensional dict
dict = {'India': {'captain': 'Virat', 'Batsman': 'Rohit', 
                  'Bolwer': 'Bumrah'},
        'England': {'captain': 'Root', 'Batsman': 'Buttler',
                    'Bolwer': 'anderson'},
        'Australia': {'captain': 'Paine',
                      'Batsman': 'Warner', 'Bolwer': 'Starck'},
        'Pakistan': {'captain': 'Babar', 'Batsman': 'Hafiz', 
                     'Bolwer': 'Aamir'},
        'West Indies': {'captain': 'Pollard', 'Batsman': 'Gayle', 
                        'Bolwer': 'Narayan'}
        }
  
# find fielder for india
# return Not Found if not exists in dict
print(dict.get('India').get('Fielder', 'Not Found'))


Python3
# Example of dict.get() with multidimensional dict
dict = {'India': {'captain': 'Virat', 'Batsman': 'Rohit',
                  'Bolwer': 'Bumrah'},
        'England': {'captain': 'Root', 'Batsman': 'Buttler', 
                    'Bolwer': 'anderson'},
        'Australia': {'captain': 'Paine', 'Batsman': 'Warner', 
                      'Bolwer': 'Starck'},
        'Pakistan': {'captain': 'Babar', 'Batsman': 'Hafiz',
                     'Bolwer': 'Aamir'},
        'West Indies': {'captain': 'Pollard', 'Batsman': 'Gayle',
                        'Bolwer': 'Narayan'}
        }
  
# find batsman for new zealand
# return Not Found if not exists in dict
# if new zealand not found in dict will result in error
print(dict.get('new zealand').get('Batsman', 'Not Found'))


Python3
# Example of dict.get() with multidimensional dict
dict = {'India': {'captain': 'Virat', 'Batsman': 'Rohit', 
                  'Bolwer': 'Bumrah'},
        'England': {'captain': 'Root', 'Batsman': 'Buttler',
                    'Bolwer': 'anderson'},
        'Australia': {'captain': 'Paine', 'Batsman': 'Warner',
                      'Bolwer': 'Starck'},
        'Pakistan': {'captain': 'Babar', 'Batsman': 'Hafiz',
                     'Bolwer': 'Aamir'},
        'West Indies': {'captain': 'Pollard', 'Batsman': 'Gayle',
                        'Bolwer': 'Narayan'}
        }
  
# find batsman for new zealand
# return Not Found if not exists in dict
print(dict.get('new zealand',{}).get('Batsman', 'Not Found'))


Python3
# use of dict.get() in multidimensional dictionary
  
dict = {'emp1': {'Name': {'First Name': 'Joe', 
                          'Last Name': 'tribiani'}, 
                 'age': 32},
        'emp2': {'Name': {'First Name': 'Mark',
                          'Last Name': 'Adam'},
                 'age': 20},
        'emp3': {'Name': {'First Name': 'luci', 
                          'Last Name': 'truk'},
                 'age': 47},
        }
  
# return Not Found if emp2 is not found
# or Name is not Found or Last name is not found
print(dict.get('emp2', {}).get('Name', {}).get('Last Name',
                                               'Not Found'))


输出:

34

示例 2:

现在,让我们看看如何将 dict.get() 与多维字典一起使用。对于多维字典,我们在单个语句中多次使用 .get()。

Python3

# Example of dict.get() with multidimensional dict
dict = {'India': {'captain': 'Virat', 'Batsman': 'Rohit', 
                  'Bolwer': 'Bumrah'},
        'England': {'captain': 'Root', 'Batsman': 'Buttler',
                    'Bolwer': 'anderson'},
        'Australia': {'captain': 'Paine', 'Batsman': 'Warner',
                      'Bolwer': 'Starck'},
        'Pakistan': {'captain': 'Babar', 'Batsman': 'Hafiz',
                     'Bolwer': 'Aamir'},
        'West Indies': {'captain': 'Pollard', 'Batsman': 'Gayle',
                        'Bolwer': 'Narayan'}
        }
  
# find batsman for india
# return Not Found if not exists in dict
print(dict.get('India').get('Batsman', 'Not Found'))

输出:

Rohit

示例 3:

你可以看到它给出了正确的输出。让我们了解这个 dict.get() 的工作原理。首先 dict.get() 返回键 'India' 的所有值,这是一个字典。它现在返回 {'captain':'Virat','Batsman':'Rohit','Bolwer':'Bumrah'} 对于这本字典,我们再次使用 get() 方法来查找值。所以对于“蝙蝠侠”,它重新播放了“Rohit”。

Python3

# Example of dict.get() with multidimensional dict
dict = {'India': {'captain': 'Virat', 'Batsman': 'Rohit', 
                  'Bolwer': 'Bumrah'},
        'England': {'captain': 'Root', 'Batsman': 'Buttler',
                    'Bolwer': 'anderson'},
        'Australia': {'captain': 'Paine',
                      'Batsman': 'Warner', 'Bolwer': 'Starck'},
        'Pakistan': {'captain': 'Babar', 'Batsman': 'Hafiz', 
                     'Bolwer': 'Aamir'},
        'West Indies': {'captain': 'Pollard', 'Batsman': 'Gayle', 
                        'Bolwer': 'Narayan'}
        }
  
# find fielder for india
# return Not Found if not exists in dict
print(dict.get('India').get('Fielder', 'Not Found'))

输出:

Not Found

示例 4:

您可以看到“印度”返回的字典中不存在外野手。这就是为什么它给出“未找到”作为输出。但是使用这个 get() 方法有一个问题。如果未找到与第一个键对应的值,则它将返回一个字符串,第二个 get() 方法将应用于该字符串。所以这将给出一个错误,因为 dict.get() 是字典而不是字符串的方法。

Python3

# Example of dict.get() with multidimensional dict
dict = {'India': {'captain': 'Virat', 'Batsman': 'Rohit',
                  'Bolwer': 'Bumrah'},
        'England': {'captain': 'Root', 'Batsman': 'Buttler', 
                    'Bolwer': 'anderson'},
        'Australia': {'captain': 'Paine', 'Batsman': 'Warner', 
                      'Bolwer': 'Starck'},
        'Pakistan': {'captain': 'Babar', 'Batsman': 'Hafiz',
                     'Bolwer': 'Aamir'},
        'West Indies': {'captain': 'Pollard', 'Batsman': 'Gayle',
                        'Bolwer': 'Narayan'}
        }
  
# find batsman for new zealand
# return Not Found if not exists in dict
# if new zealand not found in dict will result in error
print(dict.get('new zealand').get('Batsman', 'Not Found'))

输出:

Traceback (most recent call last):
  File "/home/4deb2392dee0ab15dec836bca68a69e2.py", line 12, in 
    print(dict.get('new zealand').get('Batsman', 'Not Found'))
AttributeError: 'NoneType' object has no attribute 'get'

这会给出“NoneType”对象错误,因为字符串数据类型没有名为 get() 的方法。因此,为了解决这个错误,我们将使用 get() 方法的第二个参数,如果在字典中找不到键,则默认输出。如果字典中不存在键,我们将返回一个空字典。

Python3

# Example of dict.get() with multidimensional dict
dict = {'India': {'captain': 'Virat', 'Batsman': 'Rohit', 
                  'Bolwer': 'Bumrah'},
        'England': {'captain': 'Root', 'Batsman': 'Buttler',
                    'Bolwer': 'anderson'},
        'Australia': {'captain': 'Paine', 'Batsman': 'Warner',
                      'Bolwer': 'Starck'},
        'Pakistan': {'captain': 'Babar', 'Batsman': 'Hafiz',
                     'Bolwer': 'Aamir'},
        'West Indies': {'captain': 'Pollard', 'Batsman': 'Gayle',
                        'Bolwer': 'Narayan'}
        }
  
# find batsman for new zealand
# return Not Found if not exists in dict
print(dict.get('new zealand',{}).get('Batsman', 'Not Found'))

输出:

Not Found

在输出中,即使在字典中找不到第一个键,您也可以看到它完美地工作。我们还将对更高维度使用相同的方法。现在让我们看看我们是否可以将它用于更高的维度。

Python3

# use of dict.get() in multidimensional dictionary
  
dict = {'emp1': {'Name': {'First Name': 'Joe', 
                          'Last Name': 'tribiani'}, 
                 'age': 32},
        'emp2': {'Name': {'First Name': 'Mark',
                          'Last Name': 'Adam'},
                 'age': 20},
        'emp3': {'Name': {'First Name': 'luci', 
                          'Last Name': 'truk'},
                 'age': 47},
        }
  
# return Not Found if emp2 is not found
# or Name is not Found or Last name is not found
print(dict.get('emp2', {}).get('Name', {}).get('Last Name',
                                               'Not Found'))

输出:

Adam

所以这很完美。我们可以将它用于任何维度。如果在字典中找不到键,只需添加字典作为默认返回值。添加字典作为默认返回值,除了最后一个 get() 方法,否则它将输出一个空字典。如果找不到该值,请在最后一个 get() 方法中添加要显示的任何字符串。