銆愇?婕旂畻鍦?PY銆戞墧鎺夋父鏍囧崱灏哄悗鐢?PY 鎷兼帴浠g爜

Posted aswr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆愇?婕旂畻鍦?PY銆戞墧鎺夋父鏍囧崱灏哄悗鐢?PY 鎷兼帴浠g爜相关的知识,希望对你有一定的参考价值。

<!-- 銆愇?婕旂畻鍦?PY銆戞墧鎺夋父鏍囧崱灏哄悗鐢?PY 鎷兼帴浠g爜 -->

PY 閲屽瓧绗︿覆鍐呭褰?PY 浠g爜鎵ц鐢?exec() 鎴?eval() 銆傦紙杩欑鎿嶄綔鏄笉鏄緢鍍忚嚜宸辫窡鑷繁浜ゆ槗锛燄煒忥級

  • 鍓嶈€呭彲浠ユ墽琛屼竴鑸殑浠g爜锛屼絾涓嶄細鏈夎繑鍥烇紝浣犲氨褰撴墽琛屼竴涓?.PY 鐨勮剼鏈€?/li>
  • 鍚庤€呭彲浠ユ湁杩斿洖锛屼絾鍙兘鏄竴鏉¤鍙ャ€傚棷锛屼竴鏉¤鍙ャ€傪煓?/li>

杩欎釜鎸轰笉閿欑殑锛?https://github.com/pyecharts/pyecharts 鐗瑰埆鏄浘鏍囥€?/p>

\'鎴戞案杩滃枩娆㈡捣缁靛疂瀹?

瀹冩湁涓ず渚嬨€傛垜璺熺潃杩欓噷鍜?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fgallery.pyecharts.org%2F%23%2FBar%2Fstack_bar_percent" rel="nofollow">杩欓噷绋嶅井鏀逛簡涓€涓嬶紝鎴愪簡杩欐牱锛?/p>

from pyecharts.charts import Bar
from pyecharts import options as opts

(
    Bar(init_opts=opts.InitOpts(,,page_title="HAHAHAHA"))
    .add_xaxis(["琛~", "姣涜。", "棰嗗甫", "瑁ゅ瓙", "椋庤。", "楂樿窡闉?, "琚滃瓙"])
    .add_yaxis("鍟嗗A", [114, 55, 27, 101, 125, 27, 105])
    .add_yaxis("鍟嗗B", [57, 134, 137, 129, 145, 60, 49])
    .set_global_opts(title_opts=opts.TitleOpts(title="鏌愬晢鍦洪攢鍞儏鍐?))
).render("shangjias.html")

鐒跺悗鎴戞湁浜嗕竴涓兂娉曪細鎴戞兂鑷姩鐢熸垚涓€涓腑闂撮儴鍒嗛殢鏈虹殑鍙互鎸囧畾鍟嗗鏁扮洰鐨勮繖娈典唬鐮併€?/p>

濂斤紝涓€杈规煡涓€杈瑰共锛侊紙涓€杈规煡鏄洜涓烘垜涓嶄細 PY 锛屼篃姝e洜姝ゆ墠鏈変簡杩欎唤鈥︹€︾瑪璁帮紵锛堟墧娓告爣鍗″昂鍒欐槸椤哄甫銆傛潵閮芥潵浜嗭紝灏遍『渚挎墧浜嗗槢銆傘€傘€傘€傦級锛?/p>

杈ㄦ瀽 exec 鍜?eval

鍏跺疄 Bash 涔熸湁杩欎咯鐜╂剰銆備笉杩囷紝鎴戝彧浼氱帺鍓嶈€咃紝鐢ㄦ潵妯′豢涓€涓?鍏堝畬鎴愭湰娆¤皟鐢ㄧ殑鍑芥暟鍐嶅紑濮嬩笅娆¤皟鐢?/em> 鐨勬晥鏋滐紝浠庤€屽湪 Bash 涓婂疄鐜?鍑芥暟寮?/em> 鐨勫熬閫掑綊銆傜湅 PY 閲屼篃鏈夛紝涓€寮€濮嬭繕鏈夌偣灏忔縺鍔紝浣?PY 鐨勮繘绋嬮€昏緫璺?Bash 鑲畾涓嶄竴鏍凤紝鎴戝氨濮戜笖鍏堟斁涓嬭繖涓皬婵€鍔ㄣ€?/p>

鐜板湪鐨勫ぇ涓氭槸锛岀帺 PY 涓嶇敤娓告爣鍗″昂锛屽拰锛岀敤 PY 鐜╁姩鎬佷唬鐮侊紝杩欎袱椤癸紝鍒殑銆婄◢銆嬪悗鍐嶇爺绌躲€傘€?/p>

鍏跺疄杈ㄦ瀽寰堢畝鍗曪紝鍦?PY 閲岋細

  • 鍓嶈€呭彲浠ユ墽琛屼竴鑸殑浠g爜锛屼絾涓嶄細鏈夎繑鍥烇紝浣犲氨褰撴墽琛屼竴涓?.PY 鐨勮剼鏈紝鏃犺繑鍥炪€?/li>
  • 鍚庤€呭彲浠ユ湁杩斿洖锛屼絾鍙兘鏄竴鏉¤鍙ャ€傚棷锛屼竴鏉¤鍙ャ€傪煓?/li>

Note

鍏跺疄杩欎釜璁捐寰堝悎鐞嗐€?/strong>
涓嶇劧锛屽氨浼氬儚 Powershell 閭f牱锛屼竴涓嚱鏁扮珶鐒跺彲浠ユ湁澶氫釜杩斿洖浜嗭紝閭e氨澶悡浜轰簡銆?/strong>

銆婁竴鏍风畝鍗曠殑绀轰緥銆嬶細

浣犲厛澶嶅埗涓嬮潰鐨勪唬鐮佽繘鍏ヤ綘鐨?PY 鐒跺悗鏁蹭竴涓嬪洖杞︼紝浣犲氨鐭ラ亾瀹冩剰鍛崇潃浠€涔堜簡锛?/p>

\'{}\\n{}\'.format(1,2)

鐭ラ亾涓婇潰鐨勬剰鍛崇潃浠€涔堝氨鐭ラ亾涓嬮潰鐨勬剰鍛崇潃浠€涔堜簡锛?/p>

\'{}\\n{}\'.format("\'yaya{}rara\'",".format(2)")

灏辩煡閬撲笅闈㈢殑浠g爜鎰忓懗鐫€浠€涔堜簡锛堣繑鍥炵粨鏋滀笂杩欎袱涓竴鏍蜂絾寤鸿鐢ㄥ墠鑰呭嵆 鏍煎紡鏁版嵁鍒嗗埆瀹氫箟 鍘熷垯锛夛細

\'({}\\n{})\'.format("\'yaya{}rara\'",".format(2)")
\'{}\\n{}\'.format("(\'yaya{}rara\'",".format(2))")

琛屼簡锛岀潄澶х溂鐫涳紝鎴戜滑瑕佸紑濮嬩簡锛?/p>

鎴戜滑鍏堢湅鐪?exec 鍜?eval 瑙f瀽涓嬮潰閭e潹瀛楃涓叉椂鍊欑殑鏁堟灉锛?/p>

exec(\'({}\\n{})\'.format("\'yaya{}rara\'",".format(2)")) # 娌¤繑鍥炰篃娌℃墦鍗颁篃娌℃姤閿欙紙浠g爜娌℃墦鍗颁篃娌¢敊锛?eval(\'({}\\n{})\'.format("\'yaya{}rara\'",".format(2)")) # 杩斿洖: \'yaya2rara\'

鍐嶇湅鐪嬭繖涓細

exec(\'{}\\n{}\'.format("\'yaya{}rara\'",".format(2)")) # 鎶ラ敊
eval(\'{}\\n{}\'.format("\'yaya{}rara\'",".format(2)")) # 鎶ラ敊

鎶ラ敊涓€鏍凤紝鍥犱负 PY 涓嶆敮鎸佹瘮杈冭嚜鐢辩殑鏍煎紡鍖栵紝闄ら潪鍦ㄦ嫭鍙烽噷馃檭銆傦紙杩欎篃鏄垜鐨勫彲涔樹箣鏈猴紝褰撶劧鎴戜笉鍏夌敤鍦嗘嫭鍙?() 锛屼富瑕佽繕鏄柟鎷彿 [] 鍜屾媺鎷斿搾 lambda 銆傦級

鍫嗘暟

杩欓噷鍏堢暀涓嬩竴涓皬鐤戞儜锛氫竴鑸?Java 鎴?Scala 閮藉彲浠ュ啓鍏ㄧ被鍚嶆潵閬垮厤 import 锛屼絾 PY 灏辨槸涓嶈銆傚綋鐒讹紝涔熷彲鑳芥槸杩樻湁鍒殑鎴戜笉鐭ラ亾鐨勫姙娉曘€備絾涓嶇鎬庢牱锛屾棦鐒跺姝わ紝鎴戝氨缁欏紩鍏ョ殑闅忔満閲嶆柊璧蜂釜鍚嶅瓧濂戒簡锛屽氨鍙?闅忔満 濂戒簡锛?/p>

import random as 闅忔満

Note

鍙兘鎴戦渶瑕佺壒鍒槑鐫€寮鸿皟涓€涓嬶細涓嬮潰鐨勪唬鐮佹墽琛岀殑鍓嶆彁鏄凡缁忔墽琛岃繃浜嗕笂闈㈢殑杩欎釜 import 銆?/strong>
鍏跺疄鎴戝緢涓嶅枩娆㈣繖绉嶈璁★細鎴戣寰椾竴琛屼唬鐮佽嚜甯︿簡鎵€鏈夐儴鍒嗕細姣旇緝濂斤紝鑰屼笉鏄畠鍙槸蹇呰宸ヤ綔鐨勪竴閮ㄥ垎銆傚鏋滃彧鏄竴閮ㄥ垎锛?strong>浣犺瀛╁瓙浠笂鍝効鎵惧埆鐨勯儴鍒嗗幓鍛紵瀛╁瓙浠氨瑕佷激蹇冧簡鍛€馃あ馃あ銆傘€傘€傘€?/p>

鎴戜滑鍏堟悶涓€涓殢鏈哄垪琛細

[闅忔満.randint(222,333) for i in range(12)]

鍙互澶氭墽琛屽嚑娆★紝鐪嬬湅鏍峰瓙銆備负浠€涔堣浣犵湅鐪嬪畠鐨勬牱瀛愶紵鍥犱负浣犻┈涓婂氨鍐嶄篃瑙佷笉鍒板畠浜嗐€傘€傘€傘€?/p>

鍦ㄨ涓嶅埌瀹冧箣鍓嶏紝鍏堣ˉ鍏呬竴涓嬪叧浜?lambda 鐨勭煡璇嗙偣銆備笉闅撅紝鎵ц涓嬮潰浠g爜姣旀瘮鐪嬪氨濂斤細

1+2 # 杩斿洖: 3
(lambda x,y: x+y)(1,2) # 杩斿洖: 3

1+1 # 杩斿洖: 2
(lambda x: x+x)(1) # 杩斿洖: 2

[e+7 for e in range(4)] # 杩斿洖: [7, 8, 9, 10]
(lambda x: [e+7 for e in range(x)])(4) # 杩斿洖: [7, 8, 9, 10]

[\'ahaha {} jijijijijiji\'.format(e) for e in range(4)] # 杩斿洖浣犺嚜宸辫瘯銆?(lambda x: [\'ahaha {} jijijijijiji\'.format(e) for e in range(x)])(4) # 杩斿洖浣犺嚜宸辫瘯锛堝弽姝h窡涓婇潰涓€鏍凤級銆?

杩欎箞鐪嬫潵锛屽彲鑳戒細鏈変汉璇翠簡锛?"杩欎釜 lambda 涓嶅氨鏄姝や竴涓惧槢" 锛岃繖浣犲氨涓嶇煡閬撲簡锛屽鏋滃彧璁╀綘鍐欎竴琛屼唬鐮侊紝杩欏氨涓嶈兘璧嬪€间簡锛屼絾鍙堣浣犳妸鏌愪釜闅忔満鏁扮敤涓ゆ锛屼綘鎬庝箞鍔炲憿锛?/p>

鍏堜笉璇磋繖涓紝鍙嶆鍚庨潰椹笂浼氶亣鍒般€傪煓?/p>

鑰屼笖闇€瑕佺敤鍒?lambda 鐨勫湴鏂瑰凡缁忛亣鍒颁簡锛岄偅灏辨槸鎴戜滑鎯宠鎻愬彇 鍐欏畾鐨勬暟鍊?/em> 涓?鍙橀噺 銆?/p>

(lambda cnt: [闅忔満.randint(222,333) for i in range(cnt)])(7)

杩欐牱灏辨槸瀹氫箟涓€涓嚱鏁板苟绔嬪埢璋冪敤锛岃繑鍥炵粨鏋滄槸鏈?7 涓殢鏈烘暟鐨勫垪琛紝闅忔満鏁版槸 222 鍜?333 涔嬮棿鐨?int 鏁帮紙鍗虫暣鏁帮級銆?/p>

鍙﹀锛?lambda 鍑芥暟瀹氫箟鏈韩灏辩浉褰撲簬鏄竴涓?鍐欏畾鐨勬暟鍊?/em> 锛屽彧涓嶈繃绫诲瀷涓嶆槸 int 涔熶笉鏄竴鑸殑鍒殑浠€涔堢被鍨嬨€傛€讳箣锛屽畠浼氬儚涓€涓暟鍊间竴鏍疯杩斿洖銆備粠鑰岋紝鍙互鏈変笅闈㈢殑瀹氫箟锛?/p>

lambda cnt: 
    (lambda low,high: 
        ( [闅忔満.randint(low,high) for i in range(cnt)] )
    )

璋冪敤锛?/p>

(lambda cnt: 
    (lambda low,high: 
        ( [闅忔満.randint(low,high) for i in range(cnt)] )
    )
) (7) (222,333)

杩欓噷鍏跺疄鍙戠敓浜嗕袱娆¤皟鐢細

  • 绗竴娆¤皟鐢ㄧ殑鍙傛暟鍒楄〃鏄?(7) 锛岃繑鍥炰簡涓€涓?strong>鍐呭鏄繖鏍峰瓙鐨勫€硷細 lambda low,high: ( [闅忔満.randint(low,high) for i in range(7)] )
  • 鐒跺悗瀹冨悗闈㈠張鏈夊弬鏁板垪琛?(222,333) 锛屽氨鍙堣璋冪敤浜嗕竴娆★紝杩欐璋冪敤灏辩浉褰撲簬鎵ц [闅忔満.randint(222,333) for i in range(7)] 浜嗐€?
    锛堜簨瀹炰笂锛?strong>鎵ц lambda 鍟ュ暐: 鍟ュ暐 鍛戒护鐨?strong>杩斿洖缁撴灉灏辨槸涓€涓?strong>鍐呭涓?lambda 鍟ュ暐: 鍟ュ暐 鐨?纭畾鍊?/em> 銆傦級

Note

鍏跺疄锛屾妸鍑芥暟浣滀负鍊间紶鏉ヤ紶鍘昏繖绉嶄簨锛岀敤 Lisp 绯诲垪鐨勮瑷€鍐欒捣鏉ヤ細闈炲父鐩磋銆傚彧涓嶈繃鏈枃鏄湪鐜?PY 锛屾墍浠ュ氨涓嶆墦绠楀ソ濂戒粙缁?Lisp 鐨?S 琛ㄨ揪寮?/em> 鐨勯偅绉嶄唬鐮佷簡锛屽彲浠ヨ嚜宸辨煡涓€鏌?Scheme 鐨勮娉曚互鍙?lambda 鐨勫啓娉曘€?/p>

涓婇潰鐨勮皟鐢ㄧ粨鏋滃氨鏄?涓€涓湁 7 涓殢鏈烘暟鐨勫垪琛紝闅忔満鏁版槸 222 鍜?333 涔嬮棿鐨?int 鏁帮紙鍗虫暣鏁帮級 銆?/p>

濂戒簡锛屾垜鐜板湪鎯宠鎼炲ソ鍑犱釜杩欐牱鐨勫垪琛紒

閭d箞濂斤紝鍋囪鎴戜滑鏈夎繖鏍蜂竴涓?strong>鍒楄〃锛?/p>

[
    (\'鍟嗗1\',(200,300)),
    (\'鍟嗗2\',(50,600)),
    (\'鍟嗗3\',(80,100))
]

鎺ヤ笅鏉ュ彧闇€瑕佹妸鍙宠竟鐨勫彉鎴愬弬鏁板垪琛ㄨ皟鐢ㄤ箣鍓嶇殑鍑芥暟锛岀劧鍚庢妸缁撴灉鍜屽墠闈㈢殑 \'鍟嗗x\' 鎷艰捣鏉ュ氨琛屸€︹€?/p>

杩欎笉澶燂紒鎴戣繕鎯宠姣忎釜鐨勫弬鏁拌繕閮戒笉涓€鏍凤紒鎴戜笉鎯宠嚜宸卞畾锛屽氨鎯崇敤闅忔満鏁帮紒

濂斤紝閭e氨鍏堢敓鎴愪竴涓彲浠ユ寚瀹氭暟鐩殑杩欐牱鐨勫垪琛ㄣ€?/p>

鎴戜滑闇€瑕佸 闅忔満.randint(x,y) 杩欎釜鍑芥暟浼犲叆闅忔満鐨勫弬鏁帮紝骞朵笖瑕佺‘淇濈涓€涓皬浜庣浜屼釜銆?/p>

鎴戠殑鎬濊矾鏄紝纭畾绗竴涓殢鏈哄€硷紝绗簩涓氨闅忔満鍔犱竴涓閲忋€?/strong>

鎬濊矾鏈変簡锛屽紑骞层€?/p>

棣栧厛锛屽紕涓€涓?\'鍟嗗X\' 鍚庨潰閭f牱鐨勪竴涓厓缁勫嚭鏉ワ紝闅忔満鍦帮細

(闅忔満.randint(40,300), 闅忔満.randint(40,300) + 闅忔満.randint(10,600))

绛夌瓑锛佸畬铔嬪幓鍚э紒杩欐牱瀹屽叏鏈夊彲鑳戒細璁╃浜屼釜鏁版洿灏忥紒鍥犱负涓や釜 闅忔満.randint(40,300) 瀹屽叏鍙兘涓嶆槸涓€鏍风殑鍊硷紒锛侊紒锛?/p>

鎬庝箞鍔烇紵鎴戠幇鍦ㄥ凡缁忎笉鍏佽鎴戣嚜宸辨妸鍊艰祴鍊肩粰鍙橀噺浜嗭紒鎬庝箞鍔烇紒

lambda 锛氬埆鎷呭績锛屽皬浼欏瓙锛佹兂涓€鎯宠繖涓編濡欑殑缁撴瀯锛?(lambda a (a a)) 锛堣繖涓槸 Scheme 閲岀殑鍐欐硶锛岀瓑浠风殑 Python 鍐欐硶鏄繖鏍凤細 (lambda a: a(a)) 銆傛€庝箞鏍凤紵鏄笉鏄?Scheme 姣旇緝鐗涢€硷紵馃樁锛夛紙鍙﹀杩欎釜缁撴瀯鏉ヨ嚜 Y 缁勫悎瀛?/em> 銆傦級
鍏跺疄杩欐牱涓句緥鏇寸畝鍗曪細 (lambda x: x+x) 杩欎笉灏辨槸鐢ㄤ簡涓ゆ锛佸悓涓€涓暟锛侊紒锛侊紒涓嶄俊鍙互楠岃瘉锛?(lambda x: (x,x+x))(闅忔満.randint(40,300))

鎬讳箣锛岀幇鍦ㄦ垜浠湁涓绘剰鎼炲畾涓婇潰鐨勯渶瑕佷簡锛?/p>

(lambda rndlow, add: (rndlow , rndlow + add) ) (闅忔満.randint(40,300), 闅忔満.randint(10,600) )

杩欓噷灏变笉鎶婃湰鏉ョ殑绗簩涓弬鏁版墦鍗板嚭鏉ヤ簡锛屾兂鐪嬬湅鐨勮瘽鏃犻潪鏄彲浠ヨ繖鏍凤細 (lambda rndlow, add: (rndlow , rndlow + add , add)) (闅忔満.randint(40,300), 闅忔満.randint(10,600) ) 锛屽叾涓浜屼釜鏁拌偗瀹氬氨鏄涓€涓涓変釜鐨勫拰锛屾瘡娆¤偗瀹氶兘鏄€?/p>

鐜板湪锛屼竴涓繖鐜╂剰锛堝氨鏄竴涓簩鍊煎厓缁勶級锛屾垜浠悶鍑烘潵浜嗐€傜幇鍦紝鎴戜滑闇€瑕佷竴涓垪琛紝鍒楄〃閲岋紝瑕佹湁濂藉嚑涓繖鐜╂剰銆?/p>

鎸囧畾闀垮害鐨勫垪琛ㄥ拫寮勶紵鐪嬶細

[elem*2 for elem in range(12)]
[\'heihei {} miaomiao\'.format(elem) for elem in range(12)]

[闅忔満.randint(40,300) for elem in range(12)] # 杩斿洖 12 涓?40 ~ 300 鐨勯殢鏈烘暟
[(闅忔満.randint(40,300), 闅忔満.randint(10,600)) for elem in range(12)] # 杩斿洖 12 涓殑浜屽€煎厓缁勶紝绗竴涓€兼槸 40 ~ 300 鐨勯殢鏈烘暟锛岀浜屼釜鍊兼槸 10 ~ 600 鐨勯殢鏈烘暟锛堜袱涓€肩殑鏁板€煎綋鐒跺苟娌′粈涔堝叧绯诲湪閫昏緫涓婂畠浠槸鐙珛鐫€闅忔満鐨勶級

鍚庝袱琛屽叾瀹炴渶鍚庢病鏈夌敤鍒?elem 锛岃浼犻€掍笅鍘荤殑 淇℃伅 鍙湁 elem 涓€鍏辨湁鍑犱釜 杩欎竴鐐广€?/p>

鍏跺疄杩欎釜 [鏂伴泦鍚堝崟涓厓绱犲舰寮?for 渚濇嵁鍏冪礌 in 渚濇嵁鍏冪礌鏉ユ簮闆嗗悎] 鐨勫啓娉曪紝涓€寮€濮嬫垜鍗佸垎涓嶈В锛岀湅涓嶆噦瀹冨悇涓儴鍒嗕负浠€涔堟槸杩欐牱鐨勫惈涔夛紝鍥犱负鎴戞€诲線 for 寰幆鐨勫舰寮忎笂鎯炽€傚悗鏉ヤ簡瑙?Erlang 锛岀湅鍒颁簡鍒楄〃鏋勯€犲櫒鏄繖鏍峰啓鐨勶細 [鏂伴泦鍚堝崟涓厓绱犲舰寮?|| 渚濇嵁鍏冪礌 <- 渚濇嵁鍏冪礌鏉ユ簮闆嗗悎] 鎴戝氨涓€涓嬪氨鏄庣櫧 PY 杩欑璁捐鏄摢鍎挎潵鐨勬兂娉曚簡銆?/blockquote>

琛屼簡锛屾垜鎯宠涓€涓簩鍊煎厓缁勶紝浣嗙浜屼釜涓€瀹氭瘮绗竴涓ぇ锛?/p>

[ (lambda rndlow,add: (rndlow,rndlow+add)) (闅忔満.randint(40,300), 闅忔満.randint(10,600) ) for will_not_use in range(13) ]

鏉ワ紝 13 涓€?/p>

鏍煎紡鍖栦竴涓嬪惂锛屽凡缁忓緢闀夸簡銆?/p>

[   (lambda rndlow,add: 
        (rndlow , rndlow + add)
    ) (闅忔満.randint(40, 300), 闅忔満.randint(10, 600) ) 
    for will_not_use in range(13) 
]

鍙戠幇娌★紵铏界劧鎴戜滑鏄湪鐜?PY 浣嗘槸杩欓噷灞呯劧鏄?鏍煎紡鍖栬嚜鐢?/em> 鐨勶紒锛侊紒锛?/strong>

锛堣繖鍙槸鍊熺敤 PY 鐨勭壒鎬х舰浜嗭細鎷彿閲岃繛缁┖鐧界浼氳瑙嗕负涓€涓?/strong> 鈥斺€?褰撶劧锛岃繖鏄垜鐨勪釜浜烘€荤粨锛屼笉浠h〃 PY 閭d釜濮斿憳浼氱殑鎬佸害銆傘€傘€傦級

Note

杩欓噷鎴戠殑鏍煎紡鍖栧師鍒欐槸杩欐牱鐨勶細

  • 鍊间釜鏁扮‘瀹氱殑鍒楄〃鎴栧厓缁勶紝鍓嶅悗鎷彿涓婁笅瀵归綈銆佸唴瀹瑰姞涓€娆$缉杩涗竴椤逛竴琛屻€?/li>
  • 鍒楄〃鐢熸垚鍣紝鍓嶆柟鎷彿鍚庤嫢骞茬┖鏍煎悗璺熺涓€椤癸紝鐒跺悗 for 琛ㄨ揪寮忓彟璧蜂竴琛屽苟涓€娆$缉杩涖€?/li>
  • lambda 琛ㄨ揪寮忕殑璇濓紝琚皟鐢ㄥ綋鐒惰鍖呬笂鍦嗘嫭鍙蜂簡灏便€傚畾涔夊ご鍙互灏介噺绱ц窡鐫€鍓嶅渾鎷彿锛屼篃鍙互鎹㈣锛屾崲琛岀殑璇濓紝鐩稿浜庡墠鍦嗘嫭鍙锋墍鍦ㄥ眰绾э紝鍐嶅線鍚庣缉杩涗袱灞傦紝杩欎釜鍓嶆嫭鍙峰搴旂殑鍚庢嫭鍙蜂篃鍚屾牱濡傛缂╄繘骞朵笖搴斿綋鍙湪杩欐椂鍊欐墠鍙﹁捣涓€琛岋紝闄ら潪浣犺兘璁╁畠涓婁笅瀵归綈锛涜繑鍥炲€奸儴鍒嗙殑瀹氫箟锛屽彲浠ュ彟璧蜂竴琛岋紝濡傛灉鍙﹁捣涓€琛岋紝灏辫鐩稿 lambda 澶氬線鍚庣缉杩涗竴娆°€?/li>
  • 闄や簡鍖呰9 lambda 瀹氫箟鐨勫皬鎷彿鍦ㄥ唴瀹瑰彟璧蜂竴琛岀殑鏃跺€欎互澶栵紝涓€鍒囨嫭鍙凤紝搴斿綋鎯冲姙娉曠‘淇濊涔堟í鐫€鏄竴瀵硅涔堢珫鐫€鏄竴瀵广€傚鏋滄柊澧炴柊鐨勬牸寮忓寲瑙勫垯锛屼篃鏄湪纭繚杩欎竴鏉$殑鍓嶆彁涓嬭繘琛岀殑锛屾垨鑰呭氨鏄负浜嗘洿濂界‘淇濊繖涓€鏉¤€岃繘琛屻€?/li>
  • lambda 瀹氫箟琚皟鐢ㄧ殑璇濓紝涓轰簡濂界湅锛屾渶濂界‘淇濆寘瑁瑰畾涔夌殑鍚庢嫭鍙峰拰鍙傛暟鍒楄〃涔嬮棿鏈夋墍闂撮殧锛涗絾涓轰簡鏂逛究浣犺皟璇曪紝鎵€浠ユ渶濂藉埆闂撮殧鍒颁笅涓€琛岋紝鍙敤绌烘牸闂撮殧灏卞ソ鎷夈€?/li>
  • 鍙傛暟鍒楄〃鍐呭濡傛灉鍗曠嫭鎹㈣锛屽悜鍚庝竴娆$缉杩涳紝灏炬嫭鍙峰彲浠ヨ窡鍓嶉潰鐨勯€楀彿绔栫潃瀵归綈鑰屼笉鏄窡瀵瑰簲鐨勫ご鎷彿銆傦紙杩欏簲璇ユ槸鍞竴鐨勫厑璁镐笂涓嬩竴瀵规嫭鍙蜂笉瀵瑰叾鐨勫湴鏂广€傞伒鐓ц繖涓師鍒欙紝涔熷彲浠ュ仛鍒帮細涓€鐪嬪埌涓嶅榻愮殑鎷彿锛屽氨鐭ラ亾瀹冨墠闈㈡槸鍙傛暟鍒楄〃銆傦級锛堜互鍙婏紝鍙傛暟鍒楄〃灏介噺鍒姌琛屻€傝繖鏄负浜?涓€瀵规嫭鍙风珫鎴栨í瀵归綈 鐨勫師鍒欍€傦級
  • lambda 瀹氫箟琚祴鍊肩殑鏃跺€欏彲浠ヤ笉琚嫭鍙峰寘瑁广€傚鏋滆鍖呰9锛屽垯鎸夌収鍙傛暟鍒楄〃鐨勮鍒欐潵锛屽嵆鍚戝悗涓€灞傘€傚洜涓哄墠鎷彿涓嶈兘鍙﹁捣涓€琛屻€傘€傘€?/li>
  • 澶氫釜鎹㈣浜嗙殑鍙傛暟鍒楄〃鐨勫熬鎷彿杩炵画缁撳熬鐨勬椂鍊欙紝搴旇鎸夌収灞傜骇绋嬪害鍙犲姞绌烘牸銆?/li>
  • 鍏充簬閫楀彿锛屽弬鏁板垪琛ㄩ噷姣忎釜閫楀彿鍓嶉潰璐寸潃鍚庨潰鏈変竴涓┖鏍笺€佸厓缁勪腑閫楀彿鍓嶅悗閮芥湁绌烘牸灏卞儚鎿嶄綔绗︿竴鏍枫€傝皟鐢ㄦ椂锛屽弬鏁板垪琛ㄥ姟蹇呴€楀彿鍚庢湁涓€涓┖鏍硷紝鍒殑鍒欓殢鎰忋€傛崲琛岀殑璇濆氨閮芥槸閫楀彿鍓嶉潰鏈変釜绌烘牸锛屾垜浠寜鐓ф嫭鍙峰尯鍒嗘槸涓嶆槸鍙傛暟鍒楄〃銆?/li>

鐜板湪涔熷彲浠ヨ交鏄撹缁撴灉璺熶笂闈㈤偅鍒楄〃鏇村儚锛?/p>

[   (
        \'鍟嗗{}\'.format(i+1) ,
        (lambda rndlow,add: 
            (rndlow , rndlow + add)
        ) (闅忔満.randint(40, 300), 闅忔満.randint(10, 600))
    )
    for i in range(13)
]

鏍煎紡鎹㈠悗鐪嬪氨寰堝鏄撶湅浜嗭細鍙槸 for 鍓嶉潰鐨勫唴瀹硅繘鍏ヤ簡涓€瀵瑰渾鎷彿锛堣繖瀵规槸璧峰埌寤虹珛鍏冪粍鐨勪綔鐢級锛岀劧鍚庡湪
[0] 浣嶇疆鍔犱笂浜嗕竴涓瓧绗︿覆鍊笺€傚綋鐒惰繖涓€艰繖閲屽氨鐢ㄥ埌浜嗘潵鑷?range(13) 鐨勫厓绱?i 鐨勫唴瀹广€?/p>

閭f垜鎯宠嚜宸辨寚瀹氳繖涓暟閲?13 鍛細

(lambda cnt: 
    [   (
            \'鍟嗗{}\'.format(i+1) ,
            (lambda rndlow,add: 
                (rndlow , rndlow + add)
            ) (闅忔満.randint(40, 300), 闅忔満.randint(10, 600))
        )
        for i in range(cnt)
    ]
) (13)

琛屼簡锛岀幇鍦ㄥ畠杩斿洖鐨勬槸涓垪琛紝鍒楄〃鐨?strong>涓€涓厓绱?/strong>鐨勫唴瀹瑰彲鑳芥槸杩欐牱锛?(\'鍟嗗3\', (240, 713)) 銆?/p>

鐜板湪鎴戜滑鎯冲舰鎴愯繖鏍蜂竴涓垪琛紝瀹冪殑姣忎釜鍏冪礌閮芥槸绫讳技杩欐牱 (\'鍟嗗3\', [420, 285, 495, 539, 269, 296, 293]) 鐨勶細

[   (
        sj[0] , 
        (lambda cnt: 
            lambda low,high: 
                [闅忔満.randint(low, high) for i in range(cnt)]
        ) (7) (sj[1][0], sj[1][1]) 
    )
    
    for sj in 
        (lambda cnt: 
            [   (
                    \'鍟嗗{}\'.format(i+1) ,
                    (lambda rndlow,add: 
                        (rndlow , rndlow + add)
                    ) (闅忔満.randint(40, 300), 闅忔満.randint(10, 600))
                )
                for i in range(cnt)
            ]
        )(13)
] 

鍏朵腑锛?/p>

  • 绗竴涓?[ 鍚庨潰绱ц窡鐫€鏂扮敓鍒楄〃鐨勬瘡涓厓绱犵殑璁$畻閫昏緫锛岃繖涓绠楃殑鍙橀噺渚濇嵁灏辨槸 for 鍜?in 涔嬮棿鐨勯偅涓茬帺鎰忥紝杩欎釜鐜╂剰锛堝湪杩欓噷灏辨槸鍙?sj 鐨勯偅涓帺鎰忥級鐨勫唴瀹规潵鑷簬鍚庨潰鐨勯泦鍚堢殑姣忎竴椤癸紝褰撶劧锛岃繖涓泦鍚堟槸渚濇嵁涓€涓緭鍏ユ暟鍊兼墠鑳界敓鎴愮殑銆?/li>
  • 鑰屽叧浜庡湪鏂扮敓鍒楄〃鐨勬瘡鍏冪礌鐨勯€昏緫锛岄鍏堬紝瀹冩槸涓厓缁勶紝绗竴椤规槸鍟嗗鍙凤紝鑰屽畠鍦ㄦ鍒氬ソ鑳芥牴鎹?sj[0] 鍙栧緱锛涘悗闈㈤渶瑕佹湁涓€涓?7 闀垮害鐨勯殢鏈烘暟鍒楄〃锛岀敓鎴愬畠鐨?lambda 瀹氫箟鍓嶉潰宸茬粡瀹氫箟杩囦簡锛岄渶瑕佺殑鍙傛暟鍒欏彲浠ユ牴鎹?sj[1] 杩欎釜鍏冪粍鍙栧埌锛屽叿浣撳氨鏄垎鍒彇 sj[1][0] 鍜?sj[1][1] 锛屽~鍐欏湪浼犲叆鍙傛暟闇€瑕佸啓鍦ㄧ殑閭d釜姝g‘浣嶇疆锛岀劧鍚庡嚱鏁板氨鑳芥牴鎹?strong>鍙傛暟鐨勬寚瀹?/strong>鐢熸垚鏍规嵁鍙傛暟涓庡嚱鏁板畾涔夎€屽舰鎴愮殑闅忔満鏁板垪琛?/strong>浜嗐€?/li>
  • 杩欐牱涓€鏉ワ紝鍙 sj 涓€鐩磋兘琚‘瀹氭槸浠€涔堝唴瀹癸紝鑰屼笖杩欎釜鍐呭鐨勭粨鏋勫紡浠ュ強缁撴瀯鍚勪釜浣嶇疆鐨勬暟鎹殑绫诲瀷涔熼兘鏄悎涔庤姹傜殑锛岄偅涔堟渶缁堢粨鏋滃氨鑳藉嚭鏉ヤ簡锛涘ソ宸т笉宸э紝杩欎袱鐐规垜浠殑鏁版嵁婧愰兘鑳借揪鍒般€傪煓凁煓凁煓?/li>

濡傛涓€鏉ワ紝绫讳技浜?(\'鍟嗗3\', [420, 285, 495, 539, 269, 296, 293]) 杩欐牱鐨勪竴鍫嗘暟鎹垜浠氨鑳藉鐢熸垚浜嗐€?/p>

瀹冩湁鍟ョ敤锛熷畠鐨勪袱椤瑰垎鍒竴濉笉灏辨嫾鍑轰唬鐮佹潵浜嗗悧锛?/p>

[   \'    .add_yaxis("{}",{})\\n\'.format(sj_msg[0], sj_msg[1])

    for sj_msg in 
    [   (
            sj[0] , 
            (lambda cnt: 
                lambda low,high: 
                    [闅忔満.randint(low, high) for i in range(cnt)]
            ) (7) (sj[1][0], sj[1][1]) 
        )
        
        for sj in 
            (lambda cnt: 
                [   (
                        \'鍟嗗{}\'.format(i+1) ,
                        (lambda rndlow,add: 
                            (rndlow , rndlow + add)
                        ) (闅忔満.randint(40, 300), 闅忔満.randint(10, 600))
                    )
                    for i in range(cnt)
                ]
            )(13)
    ] 
]

褰撶劧锛岃繖杩樹笉澶燂紝鎴戜滑闇€瑕佺殑鏄竴涓瓧绗︿覆銆?/p>

涓嬮潰鍙堣寮曞叆涓€涓笢瑗匡細 reduce 銆?

涓嶈繃锛岃繖閲屾垜鎯冲厛澶氳涓€鍙ワ紙涓囦竴鍚庨潰鐢ㄥ埌浜嗗憿锛夛細

鍏跺疄涔熶笉鐢ㄥ紩鍏ワ紝鍗曠函鐢?lambda 鎴戣兘鍋氬埌鎵嬪姩瀹炵幇涓€涓樊涓嶅鐨勪笢瑗匡紒

鐪嬶細

## 瀹氫箟
(lambda selfff: selfff(selfff)) ( lambda selff: lambda op, list, res: res if (list == []) else (selff(selff))(op,list[1::],op(res,list[0])) ) 

## 瀹氫箟骞惰皟鐢?(lambda selfff: selfff(selfff)) ( lambda selff: lambda op, list, res: res if (list == []) else (selff(selff))(op,list[1::],op(res,list[0])) ) (lambda x,y:x-y, [5,4,3,2,1], 6)
## 杩斿洖灏卞儚杩欎釜鐨勭粨鏋滀竴鏍凤細
6-5-4-3-2-1 # 杩斿洖: -9


### 鎶婂畾涔夊苟璋冪敤鏍煎紡鍖栦竴涓嬶細


## 杩欐槸闈為€氱敤鐨?Y 缁勫悎瀛?鐢ㄦ潵璁╁尶鍚嶅嚱鏁板彲浠ヨ嚜宸辫皟鐢ㄨ嚜宸?(lambda selfff: selfff(selfff)) (
    lambda selff: 
        lambda op,list,res: 
            
            ## 杩欎笅闈㈡槸閫€鍑洪€昏緫
            res if (list == []) 
            else (selff(selff)) (
                
                ## 杩欓噷鏄€掑綊 涓嬮潰鏄柊鐨勫弬鏁?涓婇潰鏄敓鎴愪竴涓嚜宸辩劧鍚庢墠鑳介€掑綊鎵嶈兘浼犳柊鍙傛暟
                op, list[1::], op(res, list[0]) )  ) (
    
    ## 杩欎釜鏄繖娆″畾涔夊湪璋冪敤鏃跺€欑殑鍙傛暟鍒楄〃: 浼犲叆閫昏緫鏄墠鍑忓幓鍚庣粨鏋滃啀鍑忓幓鍚庛€佸垪琛ㄦ槸绗簩椤广€佸垵濮嬪€兼槸绗笁椤广€?    lambda x,y:x-y , 
    [5,4,3,2,1] , 
    6 )


### 鍝庯紝杩欐湁浜哄彲鑳戒細璇翠簡锛岃锛氫綘杩欎笉鏄姝や竴涓句簡鍢涳紝鐩存帴鍐欑畻寮忎笉灏辫浜嗗槢锛?### 杩欐垜灏辫璇翠簡锛岃繖閲屾垜浼犲叆鐨勫垪琛ㄦ槸鍐欏ソ浜嗙殑锛屽鏋滄槸渚濇嵁鍙橀噺鍔ㄦ€佺敓鎴愮殑鍒楄〃鍛紵
### 锛堟槸涓嶆槸鏈夌偣鍔ㄦ€佺敓鎴愪唬鐮侀偅鍛冲効浜嗭紵鍙笉杩囪繖閲屼笉鏄姞鏁板瓧鑰屾槸鎺ュ瓧绗︿覆锛屽綋鐪熷氨鏄湪鍔ㄦ€佺敓鎴愪唬鐮佷簡銆傘€傘€傦級
### 馃檭馃檭馃檪馃檲馃Ζ馃

涓婇潰鐨勯儴鍒嗕笉闇€瑕佸畬鍏ㄦ帉鎻★紝涓昏杩樻槸鑷繁鎵ц鐪嬬湅缁撴灉灏卞ソ锛堝彲浠ユ妸鏈€涓嬮潰鐨勫弬鏁伴殢渚挎崲涓€鎹㈣瘯璇曠湅锛夈€?
杩欓儴鍒嗘垜鍙槸鎯宠鏄庝竴涓嬶紝鑱氬悎鍑芥暟鏄彲浠ュ彧閫氳繃 lambda 婕旂畻鏉ュ畾涔夌殑锛屽苟缁欏嚭绀轰緥銆傘€傘€備竾涓€鍚庨潰鑳界敤鍒板氨鏇村ソ浜嗐€傪煓?/strong>

涓嬮潰杩樻槸鐢ㄥ紩鍏ョ殑 reduce 銆?/p>

寮曞叆鐨勮瘽锛岃€佹儻渚嬶紝鎹釜鍚嶅効銆?/p>

from functools import reduce as 姹囨€?

璇锋敞鎰忥紒瑕佹湁杩欎釜寮曞叆锛岀劧鍚庢墠鑳芥墽琛屼笅闈㈢殑浠g爜锛侊紒

鍏堜娇鐢ㄤ竴涓嬭繖涓嚱鏁扮湅鐪嬫晥鏋滐細

姹囨€?(
    lambda x,y:x+y ,
    [   \'    .add_yaxis("{}",{})\\n\'.format(sj_msg[0], sj_msg[1])

        for sj_msg in 
        [   (
                sj[0] , 
                (lambda cnt: 
                    lambda low,high: 
                        [闅忔満.randint(low, high) for i in range(cnt)]
                ) (7) (sj[1][0], sj[1][1]) 
            )
            
            for sj in 
                (lambda cnt: 
                    [   (
                            \'鍟嗗{}\'.format(i+1) ,
                            (lambda rndlow,add: 
                                (rndlow , rndlow + add)
                            ) (闅忔満.randint(40, 300), 闅忔満.randint(10, 600))
                        )
                        for i in range(cnt)
                    ]
                )(13)
        ] 
    ] )

杩欓噷鐨勮繑鍥炲€间笉浼氭湁杞箟锛屼笉杩囨病浜嬶紝绱ф帴鐫€浼氭湁鏁堟灉鏇寸洿瑙傜殑棰勮銆傝繖閲屽彧闇€瑕佺‘璁ゆ槸涓嶆槸鎴愬姛杩斿洖涓€涓姞鎬诲ソ鐨勫瓧绗︿覆灏辫浜嗐€?/p>

鏇村ソ鐨勯瑙堟柟妗堬細

x = lambda : 姹囨€?(
    lambda x,y:x+y ,
    [   \'    .add_yaxis("{}",{})\\n\'.format(sj_msg[0], sj_msg[1])

        for sj_msg in 
        [   (
                sj[0] , 
                (lambda cnt: 
                    lambda low,high: 
                        [闅忔満.randint(low, high) for i in range(cnt)]
                ) (7) (sj[1][0], sj[1][1]) 
            )
            
            for sj in 
                (lambda cnt: 
                    [   (
                            \'鍟嗗{}\'.format(i+1) ,
                            (lambda rndlow,add: 
                                (rndlow , rndlow + add)
                            ) (闅忔満.randint(40, 300), 闅忔満.randint(10, 600))
                        )
                        for i in range(cnt)
                    ]
                )(13)
        ] 
    ] )
print(x())
print(x())
print(x())

杩欓噷鐨?print(x()) 鍙互澶氭墽琛屽嚑娆°€備綘浼氱湅鍒版暟鍦ㄥ彉锛屽洜涓烘瘡娆¢兘鏄噸鏂颁粠澶存潵涓€閬嶈繖涓繃绋嬨€?/p>

杩欓噷璧嬪€肩粰 x 鍙槸涓轰簡鏂逛究涓嬮潰鐨?print 鍑芥暟銆傛瘯绔?Python 鐨?REPL 鎸変笂鐨勮瘽骞朵笉鏄€昏緫鎰忎箟涓婄殑涓€琛岄兘浼氬嚭鏉ワ紝骞朵笉浼氬儚 Zsh 鎴栬€呭摢鎬曟槸 Bash 閭f牱銆?/p>

鐜板湪锛岃祴鍊肩粰 x 鐨勬渶澶栧眰鐨?lambda 瀹氫箟鏄棤鍙傜殑銆傝涓嶈鏈変竴涓嬪憿锛熷搸锛岃繖涓晢瀹舵暟閲忔垜鎯冲湪杩欐帶鍒讹紒

閭d箞灏辨妸閲岄潰鐨勭浉搴旂殑鎶借薄鎸埌澶栭潰鏉ャ€傛暣涓畾涔夊氨鍙樻垚浜嗚繖鏍凤細

lambda cnt : 姹囨€?(
    lambda x,y:x+y ,
    [   \'    .add_yaxis("{}",{})\\n\'.format(sj_msg[0], sj_msg[1])

        for sj_msg in 
        [   (
                sj[0] , 
                (lambda cnt: 
                    lambda low,high: 
                        [闅忔満.randint(low, high) for i in range(cnt)]
                ) (7) (sj[1][0], sj[1][1]) 
            )
            
            for sj in 
                [   (
                        \'鍟嗗{}\'.format(i+1) ,
                        (lambda rndlow,add: 
                            (rndlow , rndlow + add)
                        ) (闅忔満.randint(40, 300), 闅忔満.randint(10, 600))
                    )
                    for i in range(cnt)
                ]
        ] 
    ] )

璋冪敤锛?/p>

(lambda cnt : 姹囨€?(
    lambda x,y:x+y ,
    [   \'    .add_yaxis("{}",{})\\n\'.format(sj_msg[0], sj_msg[1])

        for sj_msg in 
        [   (
                sj[0] , 
                (lambda cnt: 
                    lambda low,high: 
                        [闅忔満.randint(low, high) for i in range(cnt)]
                ) (7) (sj[1][0], sj[1][1]) 
            )
            
            for sj in 
                [   (
                        \'鍟嗗{}\'.format(i+1) ,
                        (lambda rndlow,add: 
                            (rndlow , rndlow + add)
                        ) (闅忔満.randint(40, 300), 闅忔満.randint(10, 600))
                    )
                    for i in range(cnt)
                ]
        ] 
    ] )
) (4)

鐪嬬湅缁撴灉锛屾病闂銆?/p>

鐜板湪鍙互鎰夊揩鐜╄€嶄簡锛?/p>

x = lambda cnt : 姹囨€?(
    lambda x,y:x+y ,
    [   \'    .add_yaxis("{}",{})\\n\'.format(sj_msg[0], sj_msg[1])

        for sj_msg in 
        [   (
                sj[0] , 
                (lambda cnt: 
                    lambda low,high: 
                        [闅忔満.randint(low, high) for i in range(cnt)]
                ) (7) (sj[1][0], sj[1][1]) 
            )
            
            for sj in 
                [   (
                        \'鍟嗗{}\'.format(i+1) ,
                        (lambda rndlow,add: 
                            (rndlow , rndlow + add)
                        ) (闅忔満.randint(40, 300), 闅忔満.randint(10, 600))
                    )
                    for i in range(cnt)
                ]
        ] 
    ] )
print(x(1))
print(x(2))
print(x(3))
print(x(0))

銆傘€傘€備笉鑳芥槸 0 锛屽洜涓?reduce 浼犲叆鐨勫垪琛ㄤ笉鑳戒负绌猴紙鍥犱负 reduce 涓€鑸殑閫昏緫灏辨槸鎶婄涓€涓仛鍒濆鍊肩劧鍚庢妸鍓╀笅鐨勫垪琛ㄥ拰鍒濆鍊艰繕鏈夎绠楄鍒欓兘浼犲叆鍙︿竴涓彲浠ュ畾涔夊垵濮嬪€肩殑鑱氬悎鍑芥暟銆傦級

鍒氬ソ锛岃瘯璇曟垜鍓嶉潰鑷埗鐨勯偅涓細

x = lambda cnt : (lambda selfff: selfff(selfff)) (
    lambda selff: 
        lambda op,list,res: 
            
            res if (list == []) 
            else (selff(selff)) (
                
                op, list[1::], op(res, list[0]) )  ) (
    lambda x,y:x+y ,
    [   \'    .add_yaxis("{}",{})\\n\'.format(sj_msg[0], sj_msg[1])

        for sj_msg in 
        [   (
                sj[0] , 
                (lambda cnt: 
                    lambda low,high: 
                        [闅忔満.randint(low, high) for i in range(cnt)]
                ) (7) (sj[1][0], sj[1][1]) 
            )
            
            for sj in 
                [   (
                        \'鍟嗗{}\'.format(i+1) ,
                        (lambda rndlow,add: 
                            (rndlow , rndlow + add)
                        ) (闅忔満.randint(40, 300), 闅忔満.randint(10, 600))
                    )
                    for i in range(cnt)
                ]
        ] 
    ] , 
    \'\' )
print(x(1))
print(x(2))
print(x(3))
print(x(0))

濡傛灉鏄?0 杩斿洖灏辨槸绌恒€備负鍟ワ紵浣犵湅鎴戠粰瀹氱殑閭g涓変釜鍙傛暟涓嶅氨鏄┖瀛椾覆鍢涳紒锛?/p>

馃ぃ馃ぃ馃ぃ馃ぃ锛堝樋鍢垮樋鍢垮樋鍢垮樋鍢库€︹€︹€︹€︼級

锛堜笉杩囪繖娌″暐澶т笉浜嗙殑锛屾垜鍙槸娌¤兘瀹炵幇灏佽濂界殑 reduce 缃簡銆傘€傘€傦級

鐜板湪鍐嶆敼鏀癸紝灏辫兘鎷兼帴瀹屾暣鐨勪唬鐮佷簡銆傛垜鐩存帴涓婄粨鏋滐細

xforexec = (
    lambda cnt : 
    """
from pyecharts.charts import Bar
from pyecharts import options as opts

(
    Bar(init_opts=opts.InitOpts(,,page_title="HAHAHAHA"))
    .add_xaxis(["琛~", "姣涜。", "棰嗗甫", "瑁ゅ瓙", "椋庤。", "楂樿窡闉?, "琚滃瓙"])
{}    .set_global_opts(title_opts=opts.TitleOpts(title="鏌愬晢鍦洪攢鍞儏鍐?))
).render("shangjias.html")
    """.format(
        
        ## (-> (a) (a a))
        (lambda selfff: selfff(selfff)) (
            
            ## reduce
            lambda selff: 
                lambda op,list,res: 
                    
                    res if (list == []) 
                    else (selff(selff)) (
                        
                        op, list[1::], op(res, list[0]) )  ) (
            
            
            ## str adds
            lambda x,y:x+y , 
            
            ## str list (need to know val of cnt)
            [   \'    .add_yaxis("{}",{})\\n\'.format(sj_msg[0], sj_msg[1])

                for sj_msg in 
                [   (
                        sj[0] , 
                        (lambda cnt: 
                            lambda low,high: 
                                [闅忔満.randint(low, high) for i in range(cnt)]
                        ) (7) (sj[1][0], sj[1][1]) 
                    )
                    
                    for sj in 
                        [   (
                                \'鍟嗗{}\'.format(i+1) ,
                                (lambda rndlow,add: 
                                    (rndlow , rndlow + add)
                                ) (闅忔満.randint(40, 300), 闅忔満.randint(10, 600))
                            )
                            for i in range(cnt)
                        ]
                ] 
            ] , 
            
            ## first of adding
            \'\' )  )   )
exec(xforexec(3))

涓轰粈涔堜笉鐢?eval 锛熷洜涓鸿繖涓笢瑗挎渶鍚庢病鏈夎繑鍥烇紝鑰屾槸鍓綔鐢ㄧ敓鎴愪竴涓枃浠躲€?/p>

鍏跺疄杩欓噷鍘绘帀瀛楃涓查噷鐨勪咯 import 鐨勮瘽锛屽氨涔熻兘鐢?eval 浜嗭紝涓嶈繃瀹冩瘯绔熶笉鏄嚱鏁拌€屾槸鍓綔鐢ㄥ懡浠ら泦鍚堬紙铏借鐢熸晥鐨勫彧鏈変竴鏉★級銆?/p>

鎯宠鐪嬬湅杩欎釜鏂囦欢鐨勮瘽锛屽彲浠?strong>閫€鍑?Python REPL 鐒跺悗杩欐牱锛?/p>

python -m http.server 2341

鐒跺悗璁块棶 鑺傜偣IP:2341/shangjias.html 灏辫兘鐪嬪埌缁撴灉浜嗐€?/p>

杩欐槸涓?HTTP 浠g悊锛屼竴鑸敤浜庤皟璇曘€傜敓浜т腑寤鸿浣跨敤 Httpd 鎴栬€?Nginx 鏉ュ仛杩欎釜浜嬨€?/p>

澶囧繕

鏈枃涓€鍏辨湁涓や釜 import 锛?/p>

import random as 闅忔満
from functools import reduce as 姹囨€?

鍒嗕韩娉ㄦ槑鏉ユ簮: https://segmentfault.com/a/1190000040317072

以上是关于銆愇?婕旂畻鍦?PY銆戞墧鎺夋父鏍囧崱灏哄悗鐢?PY 鎷兼帴浠g爜的主要内容,如果未能解决你的问题,请参考以下文章

銆愬睆骞曞姞宸ュ厬鎹?鍩硅銆戞洸灞?iPad 鎵嬭〃 浼樺娍鍔犲伐

PHP鏍囧噯娉ㄩ噴鍐欐硶

CPU efficiency娴嬮噺鏍囧噯锛欴MIPS

鍗风Н灞傚鐞嗗悗鐨勭壒寰佸浘灏哄璁$畻

銆怱crum瑙嗛鍥炴斁銆戞悶浜嗕袱娆$垎娆惧渾妗岋紝绔熺劧鏈夊皬浼欎即鍗婂灏辨帓闃燂紵

(c)2006-2024 SYSTEM All Rights Reserved IT常识