简体   繁体   English

有人可以在 Python 中解释这个 dict().get 函数吗?

[英]Can someone explain this dict().get function in Python?

I'm doing a coding challenge and this particular one is to return the original argument + the ordinal number.我正在做一个编码挑战,这个特殊的挑战是返回原始参数 + 序数。 Examples:例子:

return_end_of_number(553) ➞ "553-RD" return_end_of_number(553) ➞ "553-RD"

return_end_of_number(34) ➞ "34-TH" return_end_of_number(34) ➞ "34-TH"

return_end_of_number(1231) ➞ "1231-ST" return_end_of_number(1231) ➞ "1231-ST"

return_end_of_number(22) ➞ "22-ND" return_end_of_number(22) ➞ "22-ND"

Here is the top solution:这是顶级解决方案:

def return_end_of_number(num):
    d = {1: 'ST', 2: 'ND', 3: 'RD'}
    return '{}-{}'.format(num, d.get(num%10, 'TH'))

While my thinking of solving this problem was correct, I would of never thought of writing it this way.虽然我解决这个问题的想法是正确的,但我从来没有想过这样写。 What are the elements of this part:这部分的要素是什么:

d.get(num%10, 'TH')

I see he/she is using num mod 10 to get the last digit, but is the 'TH' an argument to d.get to set a default value or something?我看到他/她正在使用 num mod 10 来获取最后一位数字,但是 'TH' 是 d.get 的参数来设置默认值还是什么?

The interpreter shell is useful for answering such questions:解释器外壳对于回答这样的问题很有用:

help(dict.get)

>>> get(self, key, default=None, /) 
>>> Return the value for key if key is in the dictionary, else default.

The num%10 just delivers the one's position number of any integer. num%10只提供任何整数的位置编号。

The dict.get(...) method takes an optional default argument: https://docs.python.org/3/library/stdtypes.html#dict.get dict.get(...)方法采用可选的默认参数: https : //docs.python.org/3/library/stdtypes.html#dict.get

get(key[, default])获取(键 [,默认])

Return the value for key if key is in the dictionary, else default .如果key在字典中,则返回key的值,否则返回default If default is not given, it defaults to None , so that this method never raises a KeyError .如果默认没有给出,则默认为None ,所以,这种方法从未引发一个KeyError

So, if we trace your example in the python repl:因此,如果我们在 python repl 中跟踪您的示例:

>>> d = {1: 'ST', 2: 'ND', 3: 'RD'}

# Notice I'm not including the default argument
>>> d.get(3)
'RD'

# The key `4` doesn't exist.
>>> d.get(4) is None
True

# If we use a default for when the key doesn't exist
>>> d.get(4, 'this is the default value')
'this is the default value'
>>> d.get(4, 'TH')
'TH'

The get() method returns the value of the item with the specified key. get() 方法返回具有指定键的项目的值。 Syntax句法

dictionary.get(keyname, value)

Here, the second argument value is optional.这里,第二个参数值是可选的。 When you pass the second argument value, if the key is not found in the dictionary, it will return the 'value' which is passed as an argument.当您传递第二个参数值时,如果在字典中找不到该键,它将返回作为参数传递的“值”。

For your solution, when key other than 1,2,3 given by (num%10) is searched using .get() method, then the second argument 'value' will be returned.对于您的解决方案,当使用 .get() 方法搜索 (num%10) 给出的 1,2,3 以外的键时,将返回第二个参数“值”。

Reference: https://www.w3schools.com/python/ref_dictionary_get.asp参考: https : //www.w3schools.com/python/ref_dictionary_get.asp

You're right, it's the default value!你是对的,这是默认值!

dict[key] throws a KeyError if the key is not found:如果找不到密钥,dict[key] 会抛出一个KeyError

>>> numbers = {'one':1, 'two':2, 'three':3}
>>> numbers['four']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'four'

dict.get(key) is nicer and returns the default None when the key is not found: dict.get(key) 更好,并在找不到密钥时返回默认值None

>>> numbers.get('four')
>>> 

However, if I wanted a custom default, I'd say dict.get(key, custom_default):但是,如果我想要自定义默认值,我会说 dict.get(key, custom_default):

>>> numbers.get('four', 'not found')
'not found'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM