楂橀樁鍑芥暟涓庡嚱鏁板紡缂栫▼

Posted 灏忚彍瀛︾紪绋?/a> 楂橀

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了楂橀樁鍑芥暟涓庡嚱鏁板紡缂栫▼相关的知识,希望对你有一定的参考价值。

鐐瑰嚮 涓婃柟钃濊壊灏忓瓧 鈥?/span> 灏忚彍瀛︾紪绋?/span> 鈥?/span> 锛?/span> 鍏虫敞鎴戜滑馃憜

浠庡墠闈㈢珷鑺傦紝鎴戜滑鐭ラ亾 Python 鍑芥暟鏄互瀵硅薄鐨勫舰寮忓疄鐜扮殑锛屽睘浜?涓€绛夊璞?/strong> ( first-class object )銆傛牴鎹紪绋嬭瑷€鐞嗚锛屼竴绛夊璞″繀椤绘弧瓒充互涓嬫潯浠讹細

  • 鍙湪杩愯鏃跺垱寤猴紱
  • 鑳借祴鍊肩粰鍙橀噺鎴栬€呮煇绉嶆暟鎹粨鏋勶紱
  • 鑳戒綔涓哄弬鏁颁紶閫掔粰鍑芥暟锛?
  • 鑳戒綔涓哄嚱鏁版墽琛岀粨鏋滆繑鍥烇紱

Python 鍑芥暟鍚屾椂婊¤冻杩欏嚑涓潯浠讹紝鍥犺€屼篃琚О涓?涓€绛夊嚱鏁?/strong> 銆?span>楂橀樁鍑芥暟 鍒欐槸鎸囬偅浜涗互鍑芥暟涓哄弬鏁帮紝鎴栬€呭皢鍑芥暟浣滀负缁撴灉杩斿洖鐨勫嚱鏁般€傚楂橀樁鍑芥暟绋嶅姞鍒╃敤锛屼究鑳界帺鍑哄緢澶氳姳鏍锋潵銆傛湰鑺備粠涓€浜涘吀鍨嬬殑妗堜緥鍏ユ墜锛岃瑙?Python 鍑芥暟楂樼骇鐢ㄦ硶銆傚悎鐞嗗簲鐢ㄥ嚱鏁板紡缂栫▼鎶€宸э紝涓嶄粎鑳借浠g爜鏇村姞绠€娲佷紭闆咃紝杩樿兘鎻愰珮寮€鍙戞晥鐜囧拰绋嬪簭璐ㄩ噺銆?/p>

鍑芥暟寮忕紪绋嬫妧宸ф渶閫傚悎鐢ㄥ湪鏁版嵁澶勭悊鍦烘櫙锛屾帴涓嬫潵浠ユ垚缁╁崟璁$畻涓轰緥锛屽睍寮€璁茶В銆傚師濮嬫暟鎹涓嬶細

scores = [
    {
        'name''灏忛洩',
        'chinese'90,
        'math'75,
        'english'85,
    },
    {
        'name''灏忔槑',
        'chinese'70,
        'math'95,
        'english'80,
    },
    {
        'name''灏忎附',
        'chinese'85,
        'math'85,
        'english'90,
    },
    {
        'name''灏忓畤',
        'chinese'85,
        'math'95,
        'english'90,
    },
    {
        'name''灏忓垰',
        'chinese'65,
        'math'70,
        'english'55,
    },
    {
        'name''灏忔柊',
        'chinese'85,
        'math'85,
        'english'80,
    },
]

sorted

鎺掑簭鏄垜浠啀鐔熸倝涓嶈繃鐨勫満鏅紝濡傛灉寰呮帓搴忓厓绱犲彲浠ョ洿鎺ユ瘮杈冿紝璋冪敤 sorted 鍑芥暟鍗冲彲锛?/p>

>>> numbers = [2869701703]
>>> sorted(numbers)
[0012367789]

瀵规瘮杈冨鏉傜殑鏁版嵁杩涜鎺掑簭锛屽垯闇€瑕佷竴浜涢澶栫殑宸ヤ綔銆傚亣濡傝鏂囪€佸笀鎯冲璇枃鎴愮哗杩涜鎺掑簭锛屾敼濡備綍杩涜鍛紵

sorted 鏀寔鎸囧畾涓€涓嚜瀹氫箟鎺掑簭鍑芥暟 key 锛岃鍑芥暟浠ュ垪琛ㄥ厓绱犱负鍙傛暟锛岃繑鍥炰竴涓€煎喅瀹氳鍏冪礌鐨勬搴忋€傜敱浜庢垜浠渶瑕佹牴鎹鏂囨垚缁╁鍏冪礌杩涜鎺掑簭锛屽洜姝ら渶瑕佸疄鐜颁竴涓嚱鏁板皢璇枃鎴愮哗鎻愬彇鍑烘潵浣滀负姣旇緝鍩哄噯锛?/p>

def by_chinese(item):
    return item['chinese']

鐜板湪鍙渶瑕佸皢 by_chinese 鍑芥暟浣滀负 key 鍙傛暟浼犵粰 sorted 鍗冲彲瀹炵幇璇枃鎴愮哗鎺掑簭锛?/p>

>>> for item in sorted(scores, key=by_chinese):
...     print(item['name'], item['chinese'])
...
灏忓垰 65
灏忔槑 70
灏忎附 85
灏忓畤 85
灏忔柊 85
灏忛洩 90

鑷畾涔夋帓搴忓嚱鏁拌繕鍙互鎺у埗鍗囬檷搴忥紝濡傛灉闇€瑕佹寜鍒嗘暟浠庨珮鍒板簳渚濇鎺掑簭锛屽彲浠ヨ繑鍥炴垚缁╃殑璐熸暟浣滀负鎺掑簭鍩哄噯锛?/p>

def by_chinese_desc(item):
    return -item['chinese']
>>> for item in sorted(scores, key=by_chinese_desc):
...     print(item['name'], item['chinese'])
...
灏忛洩 90
灏忎附 85
灏忓畤 85
灏忔柊 85
灏忔槑 70
灏忓垰 65

褰撶劧浜嗭紝閫氳繃 sorted 鍑芥暟 reverse 鍙傛暟鎺у埗鍗囬檷搴忥紝鏄竴涓洿濂界殑缂栫▼涔犳儻锛岄€昏緫鏇存竻鏅帮細

>>> for item in sorted(scores, key=by_chinese, reverse=True):
...     print(item['name'], item['chinese'])
...
灏忛洩 90
灏忎附 85
灏忓畤 85
灏忔柊 85
灏忔槑 70
灏忓垰 65

lambda

鍍?by_chinese 杩欐牱鐩存帴杩斿洖缁撴灉鐨勬瀬绠€鍑芥暟锛屽叾瀹炴病鏈夊繀瑕佸ぇ鍔ㄥ共鎴堬紝鐢?鍖垮悕鍑芥暟 瀹氫箟鍗冲彲銆?em>Python 涓殑 lambda 鍏抽敭瀛楃敤浜庡畾涔夊尶鍚嶅嚱鏁帮紝鍖垮悕鍑芥暟鍙渶缁欏嚭鍙傛暟鍒楄〃浠ュ強涓€涓〃杈惧紡浣滀负鍑芥暟杩斿洖鍊硷細

杩欐牱涓€鏉ワ紝by_chinese 杩欎釜鑷畾涔夋帓搴忓嚱鏁帮紝鍙互杩欐牱鏉ュ畾涔夛細

by_chinese = lambda item: item['chinese']

鐩稿簲鍦帮紝鎴戜滑瀹炵幇璇枃鎴愮哗鎺掑簭鐨勪唬鐮佺紪绋嬭繖鏍峰瓙锛?/p>

>>> for item in sorted(scores, key=lambda item: item['chinese']):
...     print(item['name'], item['chinese'])
...
灏忓垰 65
灏忔槑 70
灏忎附 85
灏忓畤 85
灏忔柊 85
灏忛洩 90

鏁板鑰佸笀鏉ヤ簡锛屼篃鍙渶瑕佹敼鍔ㄤ竴鐐圭偣锛屽氨鑳藉疄鐜版暟瀛︽垚缁╂帓搴忎簡锛?/p>

>>> for item in sorted(scores, key=lambda item: item['math']):
...     print(item['name'], item['math'])
...
灏忓垰 70
灏忛洩 75
灏忎附 85
灏忔柊 85
灏忔槑 95
灏忓畤 95

鍑芥暟寮忕紪绋嬭瑷€涓€鑸兘浼氭彁渚?map 銆?filter 浠ュ強 reduce 杩?3 涓珮闃跺嚱鏁帮紝鍐嶅鏉傜殑鏁版嵁缁熻澶勭悊浠诲姟閮藉彲浠ヨ浆鎹㈡垚杩欎簺绠楀瓙鐨勭粍鍚堛€傚洜姝わ紝涓嶅皯澶ф暟鎹钩鍙帮紝渚嬪 Hadoop 绛夛紝閮戒互 map 銆?reduce 涓哄熀纭€绠楀瓙銆?/p>

Python 鍐呴儴涔熻嚜甯︿簡杩欏嚑涓珮闃跺嚱鏁帮紝閭d箞杩欏嚑涓珮闃跺嚱鏁板浣曚笌鍩虹绠楀瓙缁勫悎锛屾墠鑳借扛鍙戝法澶у▉鍔涘憿锛熺偣鍑烩€?span class="mq-216">闃呰鍘熸枃鈥濓紝鑾峰彇鏇村璇︽儏锛?/p>

馃槝  鍘熷垱涓嶆槗锛屾眰璧?/span>锛屾眰鍦ㄧ湅

以上是关于楂橀樁鍑芥暟涓庡嚱鏁板紡缂栫▼的主要内容,如果未能解决你的问题,请参考以下文章

鍗侊細楂橀樁鍑芥暟鍜岄€掑綊鍑芥暟

鏋佸婕敾锛氬嚱鏁板紡缂栫▼鐨勪笘鐣?| Linux 涓浗

棰樺璇濓細2020 骞磋鎬庝箞鐢?Intellij IDEA 鎼缓 Haskell 鐜

鍑芥暟寮忕紪绋嬩腑鐨勫壇浣滅敤姒傚康

鐖变笂Python(10): 鍑芥暟寮忕紪绋?