为什么key=没有被记录文档?

huangapple go评论61阅读模式
英文:

Why hasnt key= been documented?

问题

Here is the translated content:

我是个初学者,所以我可能在问这个问题时完全错了,但我正在跟着一个课程,对如何使用函数对字典进行排序感到困惑,该函数返回字典中的键。我不得不进行一些测试,以查看可以在 key= 上使用哪些条件,我意识到我可以在 max 中使用它,但在尝试在 sort 中使用它时我感到困惑,我在网上搜索了如何使用 key=,但我得到的都是 keys(),我是否缺少一些理解,或者这是一些文档不够详尽的东西?

我尝试过的代码:

def name_alphabetical(student):
    return student["name"]
students = [
    {"name": "Hannah", "grade_average": 83},
    {"name": "Charlie", "grade_average": 91},
    {"name": "Peter", "grade_average": 85},
    {"name": "Rachel", "grade_average": 79},
    {"name": "Lauren", "grade_average": 92}
]

print(students.sort(key=name_alphabetical))

有效的代码:

def n(student):
    return student["name"]

students = [
    {"name": "Hannah", "grade_average": 83},
    {"name": "Charlie", "grade_average": 91},
    {"name": "Peter", "grade_average": 85},
    {"name": "Rachel", "grade_average": 79},
    {"name": "Lauren", "grade_average": 92}
]

students.sort(key=n)
print(students)

期望的结果:

[{'name': 'Charlie', 'grade_average': 91}, {'name': 'Hannah', 'grade_average': 83}, {'name': 'Lauren', 'grade_average': 92}, {'name': 'Peter', 'grade_average': 85}, {'name': 'Rachel', 'grade_average': 79}]
英文:

im a begginer so i might be straight up wrong in asking this but i was following a course and i was confused on how to sort a dictionary using a function, with the function returning the key inside that dictionary. iv had to do some testing to see what conditions i can use the key= for, i realised i could use it on max, but when trying to use it with sort i was stumped, i looked online for how to use key= and all i got was keys(), am i missing some understanding or has this been something not well doccumented?

what i tried:

def name_alphabetical(student):
    return student["name"]
students = [
    {"name": "Hannah", "grade_average": 83},
    {"name": "Charlie", "grade_average": 91},
    {"name": "Peter", "grade_average": 85},
    {"name": "Rachel", "grade_average": 79},
    {"name": "Lauren", "grade_average": 92}
]

print(students.sort(key(name_alphabetical))

what worked:

def n(student):
    return student["name"]


students = [
    {"name": "Hannah", "grade_average": 83},
    {"name": "Charlie", "grade_average": 91},
    {"name": "Peter", "grade_average": 85},
    {"name": "Rachel", "grade_average": 79},
    {"name": "Lauren", "grade_average": 92}
]

students.sort(key=n)
print(students)

resault seeked:

[{'name': 'Charlie', 'grade_average': 91}, {'name': 'Hannah', 'grade_average': 83}, {'name': 'Lauren', 'grade_average': 92}, {'name': 'Peter', 'grade_average': 85}, {'name': 'Rachel', 'grade_average': 79}]

答案1

得分: 2

已记录在sorted()中:

sorted(iterable, /, *, key=None, reverse=False)

key 指定一个接受一个参数的函数,用于从 iterable 中的每个元素中提取比较键(例如,key=str.lower)。默认值为 None(直接比较元素)。

以及list.sort()中:

sort(*, key=None, reverse=False)

key 指定一个接受一个参数的函数,用于从每个列表元素中提取比较键(例如,key=str.lower)。列表中每个项目对应的键值只计算一次,然后用于整个排序过程。默认值为 None,意味着直接排序列表项,而不计算单独的键值。

您还可以使用 REPL 查看更简洁版本的文档:

>>> help(list.sort)

Help on method_descriptor:

sort(self, /, *, key=None, reverse=False)

    将列表按升序排序并返回 None。
    
    排序是就地进行的(即列表本身被修改),并且是稳定的(即保持两个相等元素的顺序)。
    
    如果提供了一个键函数,将其一次应用于每个列表项并对它们进行排序,根据它们的函数值升序或降序排列。
    
    可以设置 reverse 标志以按降序排序。
英文:

It is documented for sorted():

> sorted(iterable, /, *, key=None, reverse=False)
>
> key specifies a function of one argument that is used to extract a comparison key from each element in iterable (for example, key=str.lower). The default value is None (compare the elements directly).

and list.sort():

> sort(*, key=None, reverse=False)
>
> key specifies a function of one argument that is used to extract a comparison key from each list element (for example, key=str.lower). The key corresponding to each item in the list is calculated once and then used for the entire sorting process. The default value of None means that list items are sorted directly without calculating a separate key value.

You can also use the REPL for a more compact version of the docs:

>
> >>> help(list.sort)
>
> Help on method_descriptor:
>
> sort(self, /, *, key=None, reverse=False)
>
> Sort the list in ascending order and return None.
>
> The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
> order of two equal elements is maintained).
>
> If a key function is given, apply it once to each list item and sort them,
> ascending or descending, according to their function values.
>
> The reverse flag can be set to sort in descending order.
>

huangapple
  • 本文由 发表于 2023年6月9日 07:25:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定