1289

Python3之max key参数学习记录

<h2>Python3之max key参数学习记录</h2>

转自https://www.cnblogs.com/zhangwei22/p/9892422.html

今天用Python写脚本,想要实现这样的功能:对于给定的字典,返回其中Value最大值对应的Key。

搜索后找到了解决方法,同时也学到了max key参数的作用。

例1,

<table style="height: 38px; width: 359px;" border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 </td> <td class="code"> <code class="python plain">testlist <code class="python keyword">= <code class="python plain">[<code class="python value">9.2<code class="python plain">, <code class="python value">10<code class="python plain">, <code class="python keyword">-<code class="python value">20.3<code class="python plain">, <code class="python keyword">-<code class="python value">7.0<code class="python plain">, <code class="python value">9.999<code class="python plain">, <code class="python value">20.111<code class="python plain">] <code class="python functions">print<code class="python plain">(<code class="python functions">max<code class="python plain">(testlist, key<code class="python keyword">=<code class="python functions">abs<code class="python plain">))  <code class="python comments">#返回 -20.3 </td> </tr></tbody></table>

获取testlist中绝对值最大的元素。

key关键字的作用是,对每个testlist元素先使用key指定的function来处理,然后再比较、返回预期的元素。

key参数的值也可以使用自定义函数。

例2,

<table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 3 4 </td> <td class="code"> <code class="python keyword">def <code class="python plain">func(n): <code class="python spaces">    <code class="python keyword">return <code class="python functions">abs<code class="python plain">(n)<br> <code class="python plain">testlist <code class="python keyword">= <code class="python plain">[<code class="python value">9.2<code class="python plain">, <code class="python value">10<code class="python plain">, <code class="python keyword">-<code class="python value">20.3<code class="python plain">, <code class="python keyword">-<code class="python value">7.0<code class="python plain">, <code class="python value">9.999<code class="python plain">, <code class="python value">20.111<code class="python plain">] <code class="python functions">print<code class="python plain">(<code class="python functions">max<code class="python plain">(testlist, key<code class="python keyword">=<code class="python plain">func))  <code class="python comments">#结果与例1一致  </td> </tr></tbody></table>

该func是自定义的函数。

对于简单的方法,一般使用lambda函数。

例3,

<table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 </td> <td class="code"> <code class="python plain">testlist <code class="python keyword">= <code class="python plain">[<code class="python value">9.2<code class="python plain">, <code class="python value">10<code class="python plain">, <code class="python keyword">-<code class="python value">20.3<code class="python plain">, <code class="python keyword">-<code class="python value">7.0<code class="python plain">, <code class="python value">9.999<code class="python plain">, <code class="python value">20.111<code class="python plain">] <code class="python functions">print<code class="python plain">(<code class="python functions">max<code class="python plain">(testlist, key<code class="python keyword">=<code class="python keyword">lambda <code class="python plain">x: <code class="python functions">abs<code class="python plain">(x)))  <code class="python comments">#结果与例1、例2一致 </td> </tr></tbody></table>

使用lambda函数作为key的参数。

例4,

<table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 3 </td> <td class="code"> <code class="python plain">probabilities_dict <code class="python keyword">= <code class="python plain">{<code class="python string">'Sunny'<code class="python plain">: <code class="python value">0.30<code class="python plain">, <code class="python string">'Cloudy'<code class="python plain">: <code class="python value">0.35<code class="python plain">, <code class="python string">'Windy'<code class="python plain">: <code class="python value">0.15<code class="python plain">, <code class="python string">'Rainy'<code class="python plain">: <code class="python value">0.20<code class="python plain">} <code class="python functions">print<code class="python plain">(<code class="python functions">max<code class="python plain">(probabilities_dict, key<code class="python keyword">=<code class="python keyword">lambda <code class="python plain">x: probabilities_dict[x]))  <code class="python comments">#返回 Cloudy <code class="python functions">print<code class="python plain">(<code class="python functions">max<code class="python plain">(probabilities_dict, key<code class="python keyword">=<code class="python plain">probabilities_dict.get))  <code class="python comments">#返回 Cloudy   </td> </tr></tbody></table>

这里,对于probabilities_dict字典的每一个元素(即字典的Key),使用key参数指定的方法进行处理,然后再比较、返回预期的字典Key。

下面的这句依次打印字典的Key、dict.get(Key)的值:

<table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 </td> <td class="code"> <code class="python keyword">for <code class="python plain">key <code class="python keyword">in <code class="python plain">probabilities_dict: <code class="python spaces">    <code class="python functions">print<code class="python plain">(key, probabilities_dict.get(key), sep<code class="python keyword">=<code class="python string">': '<code class="python plain">, end<code class="python keyword">=<code class="python string">', '<code class="python plain">)  <code class="python comments">#返回 Sunny: 0.3, Cloudy: 0.35, Windy: 0.15, Rainy: 0.2, </td> </tr></tbody></table>

  

 

巨人的肩膀:

https://www.cnblogs.com/whatisfantasy/p/6273913.html

https://stackoverflow.com/questions/18296755/python-max-function-using-key-and-lambda-expression

今天用Python写脚本,想要实现这样的功能:对于给定的字典,返回其中Value最大值对应的Key。

搜索后找到了解决方法,同时也学到了max key参数的作用。

例1,

<table style="height: 38px; width: 359px;" border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 </td> <td class="code"> <code class="python plain">testlist <code class="python keyword">= <code class="python plain">[<code class="python value">9.2<code class="python plain">, <code class="python value">10<code class="python plain">, <code class="python keyword">-<code class="python value">20.3<code class="python plain">, <code class="python keyword">-<code class="python value">7.0<code class="python plain">, <code class="python value">9.999<code class="python plain">, <code class="python value">20.111<code class="python plain">] <code class="python functions">print<code class="python plain">(<code class="python functions">max<code class="python plain">(testlist, key<code class="python keyword">=<code class="python functions">abs<code class="python plain">))  <code class="python comments">#返回 -20.3 </td> </tr></tbody></table>

获取testlist中绝对值最大的元素。

key关键字的作用是,对每个testlist元素先使用key指定的function来处理,然后再比较、返回预期的元素。

key参数的值也可以使用自定义函数。

例2,

<table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 3 4 </td> <td class="code"> <code class="python keyword">def <code class="python plain">func(n): <code class="python spaces">    <code class="python keyword">return <code class="python functions">abs<code class="python plain">(n)<br> <code class="python plain">testlist <code class="python keyword">= <code class="python plain">[<code class="python value">9.2<code class="python plain">, <code class="python value">10<code class="python plain">, <code class="python keyword">-<code class="python value">20.3<code class="python plain">, <code class="python keyword">-<code class="python value">7.0<code class="python plain">, <code class="python value">9.999<code class="python plain">, <code class="python value">20.111<code class="python plain">] <code class="python functions">print<code class="python plain">(<code class="python functions">max<code class="python plain">(testlist, key<code class="python keyword">=<code class="python plain">func))  <code class="python comments">#结果与例1一致  </td> </tr></tbody></table>

该func是自定义的函数。

对于简单的方法,一般使用lambda函数。

例3,

<table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 </td> <td class="code"> <code class="python plain">testlist <code class="python keyword">= <code class="python plain">[<code class="python value">9.2<code class="python plain">, <code class="python value">10<code class="python plain">, <code class="python keyword">-<code class="python value">20.3<code class="python plain">, <code class="python keyword">-<code class="python value">7.0<code class="python plain">, <code class="python value">9.999<code class="python plain">, <code class="python value">20.111<code class="python plain">] <code class="python functions">print<code class="python plain">(<code class="python functions">max<code class="python plain">(testlist, key<code class="python keyword">=<code class="python keyword">lambda <code class="python plain">x: <code class="python functions">abs<code class="python plain">(x)))  <code class="python comments">#结果与例1、例2一致 </td> </tr></tbody></table>

使用lambda函数作为key的参数。

例4,

<table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 3 </td> <td class="code"> <code class="python plain">probabilities_dict <code class="python keyword">= <code class="python plain">{<code class="python string">'Sunny'<code class="python plain">: <code class="python value">0.30<code class="python plain">, <code class="python string">'Cloudy'<code class="python plain">: <code class="python value">0.35<code class="python plain">, <code class="python string">'Windy'<code class="python plain">: <code class="python value">0.15<code class="python plain">, <code class="python string">'Rainy'<code class="python plain">: <code class="python value">0.20<code class="python plain">} <code class="python functions">print<code class="python plain">(<code class="python functions">max<code class="python plain">(probabilities_dict, key<code class="python keyword">=<code class="python keyword">lambda <code class="python plain">x: probabilities_dict[x]))  <code class="python comments">#返回 Cloudy <code class="python functions">print<code class="python plain">(<code class="python functions">max<code class="python plain">(probabilities_dict, key<code class="python keyword">=<code class="python plain">probabilities_dict.get))  <code class="python comments">#返回 Cloudy   </td> </tr></tbody></table>

这里,对于probabilities_dict字典的每一个元素(即字典的Key),使用key参数指定的方法进行处理,然后再比较、返回预期的字典Key。

下面的这句依次打印字典的Key、dict.get(Key)的值:

<table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 </td> <td class="code"> <code class="python keyword">for <code class="python plain">key <code class="python keyword">in <code class="python plain">probabilities_dict: <code class="python spaces">    <code class="python functions">print<code class="python plain">(key, probabilities_dict.get(key), sep<code class="python keyword">=<code class="python string">': '<code class="python plain">, end<code class="python keyword">=<code class="python string">', '<code class="python plain">)  <code class="python comments">#返回 Sunny: 0.3, Cloudy: 0.35, Windy: 0.15, Rainy: 0.2, </td> </tr></tbody></table>

  

 

巨人的肩膀:

https://www.cnblogs.com/whatisfantasy/p/6273913.html

https://stackoverflow.com/questions/18296755/python-max-function-using-key-and-lambda-expression

来源:博客园

作者:汉尼拔草

链接:https://www.cnblogs.com/peanutk/p/11426223.html

Recommend