需要对最大和最小构建函数的解释 [英] need explanation for max and min building function
问题描述
我不明白python 2.7中的内置函数max
和min
I don't understand the built-in functions max
and min
in python 2.7
max("sehir")
min("sehir")
max
给出字母 "s"
和 min
给出字母 "e"
max
gives the letter "s"
and min
gives the letter "e"
推荐答案
max
和 min
作为参数(假设你只给它一个unnamedem> 参数)一个 iterable,并返回最大/最小项.
max
and min
take as parameter (given you only give it one unnamed parameter) an iterable, and returns the maximum / minimum item.
一个字符串是一个可迭代的:如果你对一个字符串进行迭代,你会得到作为字符串字符的 1-char 字符串.
A string is an iterable: if you iterate over a string, you obtain the 1-char strings that are the characters of the string.
然后 max
和 min
迭代那个可迭代对象并返回 max
imum 或 min
imum 项.对于字符串,定义了字典顺序.所以 'a' <'b'
和 'ab' >'aa'
.所以是按字典序比较,个别字符比较是ASCII码(Unicode码="post-tag" title="显示问题标记为 'python-3.x'" rel="tag">python-3.x).由于所有字符都是单字符字符串.这里我们只需要考虑ASCII 码.您可以在此处查看 ASCII 表 [wiki].
Then max
and min
iterate over that iterable and returns the max
imum or min
imum item. For a string the lexicographical order is defined. So 'a' < 'b'
, and 'ab' > 'aa'
. So it is compared lexicographically, and the individual characters are compared by ASCII code (Unicode code in python-3.x). Since all characters have are one-caracter strings. We only have to take the ASCII code into account here. You can inspect the ASCII table here [wiki].
所以 max("sehir")
将返回 's'
,因为 max(['s', 'e', 'h', 'i', 'r']) == 's'
:迭代中的最大字符.对于 min('sehir') == 'e'
,由于 min(['s', 'e', 'h', 'i', 'r']) =='e'
因为它是字符串中的最小"字符.
So max("sehir")
will return 's'
, since max(['s', 'e', 'h', 'i', 'r']) == 's'
: the maximum character in the iterable. For min('sehir') == 'e'
, since min(['s', 'e', 'h', 'i', 'r']) == 'e'
because it is the "smallest" character in the string.
这篇关于需要对最大和最小构建函数的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!