简体   繁体   English

Python3中的排序字典

[英]Sorting dictionary in Python3

I am working on an assignment for school.我正在为学校做作业。 My professor gave us some code in class and im not sure how the logic of it works.我的教授在课堂上给了我们一些代码,我不确定它的逻辑是如何工作的。

#wdcnt is dictionary with key = "some_string" and value = some_int
v = [wdcnt.values()]
v.sort()
for k in sorted(wdcnt, key = wdcnt.get, reverse = True):
    print(k,wdcnt[k])
  1. I understand the premise of storing values into a list then sorting them, but why is v not used in the for loop.我理解将值存储到列表中然后对它们进行排序的前提,但是为什么在 for 循环中没有使用 v 。 That line of code seems pointless as v is never used after.这行代码似乎毫无意义,因为 v 之后再也没有使用过。

  2. wdcnt.get does not have "()" or parameters but still works to return a key value associated with k. wdcnt.get 没有“()”或参数,但仍然可以返回与 k 关联的键值。

Please help me understand the logic behind this code fragment, thanks.请帮助我理解这段代码片段背后的逻辑,谢谢。

Note* the assignment is finished and works but to better understand I'd like to get the logic.注意* 作业已完成并有效,但为了更好地理解我想了解逻辑。

  1. v Is not used later, you can ignore it, it is indeed useless for the loop v后面没用,可以无视,对循环确实没用

  2. The argument wdcnt.get is used without parenthesis as it's passing the function as an argument (this is legal in Python):使用参数wdcnt.get不带括号,因为它将函数作为参数传递(这在 Python 中是合法的):

From the docs :文档

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). key 指定一个带一个参数的函数,用于从 iterable 中的每个元素中提取比较键(例如,key=str.lower)。 The default value is None (compare the elements directly).默认值为 None (直接比较元素)。

  1. I understand the premise of storing values into a list then sorting them, but why is v not used in the for loop.我理解将值存储到列表中然后对它们进行排序的前提,但是为什么在 for 循环中没有使用 v 。 That line of code seems pointless as v is never used after.这行代码似乎毫无意义,因为 v 之后再也没有使用过。

That's right.这是正确的。 Like khelwood said, v is unused and has no effect.就像 khelwood 所说的那样, v未使用且无效。

  1. wdcnt.get does not have "()" or parameters but still works to return a key value associated with k. wdcnt.get 没有“()”或参数,但仍然可以返回与 k 关联的键值。

wdcnt.get is the dictionary's .get method . wdcnt.get字典的 .get 方法 wdcnt.get(k) is another way of saying wdcnt[k] . wdcnt.get(k)wdcnt[k]另一种说法。

In detail:详细:

  1. sorted iterates over the keys of wdcnt. sorted迭代 wdcnt 的键。
  2. For each key k, sorted calls the specified key comparator wdcnt.get on it.对于每个键 k, sorted调用指定的键比较器wdcnt.get就可以了。
  3. wdcnt.get(k) returns the word count value associated with k. wdcnt.get(k)返回与 k 关联的字数值。 sorted then uses those values to determine how to order of the keys. sorted然后使用这些值来确定如何对键进行排序。

The first two lines are unnecessary.前两行是不必要的。 It does not need to be used.它不需要使用。 sorted built-in is a function and one of its parameters is key. sorted 内置函数是一个函数,它的一个参数是关键。 The key parameter determines how the array given to the sorted function will be sorted. key 参数决定了给定排序函数的数组将如何排序。 It is only the name of the function given to the key.它只是提供给键的函数名称。 () is not used. () 未使用。

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

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