銆愇?婕旂畻鍦?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 閲岋細 鍏跺疄杩欎釜璁捐寰堝悎鐞嗐€?/strong> 銆婁竴鏍风畝鍗曠殑绀轰緥銆嬶細 浣犲厛澶嶅埗涓嬮潰鐨勪唬鐮佽繘鍏ヤ綘鐨?PY 鐒跺悗鏁蹭竴涓嬪洖杞︼紝浣犲氨鐭ラ亾瀹冩剰鍛崇潃浠€涔堜簡锛?/p> 鐭ラ亾涓婇潰鐨勬剰鍛崇潃浠€涔堝氨鐭ラ亾涓嬮潰鐨勬剰鍛崇潃浠€涔堜簡锛?/p> 灏辩煡閬撲笅闈㈢殑浠g爜鎰忓懗鐫€浠€涔堜簡锛堣繑鍥炵粨鏋滀笂杩欎袱涓竴鏍蜂絾寤鸿鐢ㄥ墠鑰呭嵆 鏍煎紡鏁版嵁鍒嗗埆瀹氫箟 鍘熷垯锛夛細 琛屼簡锛岀潄澶х溂鐫涳紝鎴戜滑瑕佸紑濮嬩簡锛?/p> 鎴戜滑鍏堢湅鐪? 鍐嶇湅鐪嬭繖涓細 鎶ラ敊涓€鏍凤紝鍥犱负 PY 涓嶆敮鎸佹瘮杈冭嚜鐢辩殑鏍煎紡鍖栵紝闄ら潪鍦ㄦ嫭鍙烽噷馃檭銆傦紙杩欎篃鏄垜鐨勫彲涔樹箣鏈猴紝褰撶劧鎴戜笉鍏夌敤鍦嗘嫭鍙? 杩欓噷鍏堢暀涓嬩竴涓皬鐤戞儜锛氫竴鑸?Java 鎴?Scala 閮藉彲浠ュ啓鍏ㄧ被鍚嶆潵閬垮厤 鍙兘鎴戦渶瑕佺壒鍒槑鐫€寮鸿皟涓€涓嬶細涓嬮潰鐨勪唬鐮佹墽琛岀殑鍓嶆彁鏄凡缁忔墽琛岃繃浜嗕笂闈㈢殑杩欎釜 鎴戜滑鍏堟悶涓€涓殢鏈哄垪琛細 鍙互澶氭墽琛屽嚑娆★紝鐪嬬湅鏍峰瓙銆備负浠€涔堣浣犵湅鐪嬪畠鐨勬牱瀛愶紵鍥犱负浣犻┈涓婂氨鍐嶄篃瑙佷笉鍒板畠浜嗐€傘€傘€傘€?/p> 鍦ㄨ涓嶅埌瀹冧箣鍓嶏紝鍏堣ˉ鍏呬竴涓嬪叧浜? 杩欎箞鐪嬫潵锛屽彲鑳戒細鏈変汉璇翠簡锛?"杩欎釜 鍏堜笉璇磋繖涓紝鍙嶆鍚庨潰椹笂浼氶亣鍒般€傪煓?/p> 鑰屼笖闇€瑕佺敤鍒? 杩欐牱灏辨槸瀹氫箟涓€涓嚱鏁板苟绔嬪埢璋冪敤锛岃繑鍥炵粨鏋滄槸鏈? 鍙﹀锛? 璋冪敤锛?/p> 杩欓噷鍏跺疄鍙戠敓浜嗕袱娆¤皟鐢細 鍏跺疄锛屾妸鍑芥暟浣滀负鍊间紶鏉ヤ紶鍘昏繖绉嶄簨锛岀敤 Lisp 绯诲垪鐨勮瑷€鍐欒捣鏉ヤ細闈炲父鐩磋銆傚彧涓嶈繃鏈枃鏄湪鐜?PY 锛屾墍浠ュ氨涓嶆墦绠楀ソ濂戒粙缁?Lisp 鐨?S 琛ㄨ揪寮?/em> 鐨勯偅绉嶄唬鐮佷簡锛屽彲浠ヨ嚜宸辨煡涓€鏌?Scheme 鐨勮娉曚互鍙? 涓婇潰鐨勮皟鐢ㄧ粨鏋滃氨鏄?涓€涓湁 閭d箞濂斤紝鍋囪鎴戜滑鏈夎繖鏍蜂竴涓?strong>鍒楄〃锛?/p> 鎺ヤ笅鏉ュ彧闇€瑕佹妸鍙宠竟鐨勫彉鎴愬弬鏁板垪琛ㄨ皟鐢ㄤ箣鍓嶇殑鍑芥暟锛岀劧鍚庢妸缁撴灉鍜屽墠闈㈢殑 濂斤紝閭e氨鍏堢敓鎴愪竴涓彲浠ユ寚瀹氭暟鐩殑杩欐牱鐨勫垪琛ㄣ€?/p> 鎴戜滑闇€瑕佸 鎴戠殑鎬濊矾鏄紝纭畾绗竴涓殢鏈哄€硷紝绗簩涓氨闅忔満鍔犱竴涓閲忋€?/strong> 鎬濊矾鏈変簡锛屽紑骞层€?/p> 棣栧厛锛屽紕涓€涓? 绛夌瓑锛佸畬铔嬪幓鍚э紒杩欐牱瀹屽叏鏈夊彲鑳戒細璁╃浜屼釜鏁版洿灏忥紒鍥犱负涓や釜 鎬庝箞鍔烇紵鎴戠幇鍦ㄥ凡缁忎笉鍏佽鎴戣嚜宸辨妸鍊艰祴鍊肩粰鍙橀噺浜嗭紒鎬庝箞鍔烇紒 鎬讳箣锛岀幇鍦ㄦ垜浠湁涓绘剰鎼炲畾涓婇潰鐨勯渶瑕佷簡锛?/p> 杩欓噷灏变笉鎶婃湰鏉ョ殑绗簩涓弬鏁版墦鍗板嚭鏉ヤ簡锛屾兂鐪嬬湅鐨勮瘽鏃犻潪鏄彲浠ヨ繖鏍凤細 鐜板湪锛屼竴涓繖鐜╂剰锛堝氨鏄竴涓簩鍊煎厓缁勶級锛屾垜浠悶鍑烘潵浜嗐€傜幇鍦紝鎴戜滑闇€瑕佷竴涓垪琛紝鍒楄〃閲岋紝瑕佹湁濂藉嚑涓繖鐜╂剰銆?/p> 鎸囧畾闀垮害鐨勫垪琛ㄥ拫寮勶紵鐪嬶細 鍚庝袱琛屽叾瀹炴渶鍚庢病鏈夌敤鍒? 琛屼簡锛屾垜鎯宠涓€涓簩鍊煎厓缁勶紝浣嗙浜屼釜涓€瀹氭瘮绗竴涓ぇ锛?/p> 鏉ワ紝 鏍煎紡鍖栦竴涓嬪惂锛屽凡缁忓緢闀夸簡銆?/p> 鍙戠幇娌★紵铏界劧鎴戜滑鏄湪鐜?PY 浣嗘槸杩欓噷灞呯劧鏄?鏍煎紡鍖栬嚜鐢?/em> 鐨勶紒锛侊紒锛?/strong> 锛堣繖鍙槸鍊熺敤 PY 鐨勭壒鎬х舰浜嗭細鎷彿閲岃繛缁┖鐧界浼氳瑙嗕负涓€涓?/strong> 鈥斺€?褰撶劧锛岃繖鏄垜鐨勪釜浜烘€荤粨锛屼笉浠h〃 PY 閭d釜濮斿憳浼氱殑鎬佸害銆傘€傘€傦級 杩欓噷鎴戠殑鏍煎紡鍖栧師鍒欐槸杩欐牱鐨勶細 鐜板湪涔熷彲浠ヨ交鏄撹缁撴灉璺熶笂闈㈤偅鍒楄〃鏇村儚锛?/p> 鏍煎紡鎹㈠悗鐪嬪氨寰堝鏄撶湅浜嗭細鍙槸 閭f垜鎯宠嚜宸辨寚瀹氳繖涓暟閲? 琛屼簡锛岀幇鍦ㄥ畠杩斿洖鐨勬槸涓垪琛紝鍒楄〃鐨?strong>涓€涓厓绱?/strong>鐨勫唴瀹瑰彲鑳芥槸杩欐牱锛? 鐜板湪鎴戜滑鎯冲舰鎴愯繖鏍蜂竴涓垪琛紝瀹冪殑姣忎釜鍏冪礌閮芥槸绫讳技杩欐牱 鍏朵腑锛?/p> 濡傛涓€鏉ワ紝绫讳技浜? 瀹冩湁鍟ョ敤锛熷畠鐨勪袱椤瑰垎鍒竴濉笉灏辨嫾鍑轰唬鐮佹潵浜嗗悧锛?/p> 褰撶劧锛岃繖杩樹笉澶燂紝鎴戜滑闇€瑕佺殑鏄竴涓瓧绗︿覆銆?/p> 涓嬮潰鍙堣寮曞叆涓€涓笢瑗匡細 涓嶈繃锛岃繖閲屾垜鎯冲厛澶氳涓€鍙ワ紙涓囦竴鍚庨潰鐢ㄥ埌浜嗗憿锛夛細 鐪嬶細 涓婇潰鐨勯儴鍒嗕笉闇€瑕佸畬鍏ㄦ帉鎻★紝涓昏杩樻槸鑷繁鎵ц鐪嬬湅缁撴灉灏卞ソ锛堝彲浠ユ妸鏈€涓嬮潰鐨勫弬鏁伴殢渚挎崲涓€鎹㈣瘯璇曠湅锛夈€? 涓嬮潰杩樻槸鐢ㄥ紩鍏ョ殑 寮曞叆鐨勮瘽锛岃€佹儻渚嬶紝鎹釜鍚嶅効銆?/p> 璇锋敞鎰忥紒瑕佹湁杩欎釜寮曞叆锛岀劧鍚庢墠鑳芥墽琛屼笅闈㈢殑浠g爜锛侊紒 鍏堜娇鐢ㄤ竴涓嬭繖涓嚱鏁扮湅鐪嬫晥鏋滐細 杩欓噷鐨勮繑鍥炲€间笉浼氭湁杞箟锛屼笉杩囨病浜嬶紝绱ф帴鐫€浼氭湁鏁堟灉鏇寸洿瑙傜殑棰勮銆傝繖閲屽彧闇€瑕佺‘璁ゆ槸涓嶆槸鎴愬姛杩斿洖涓€涓姞鎬诲ソ鐨勫瓧绗︿覆灏辫浜嗐€?/p> 鏇村ソ鐨勯瑙堟柟妗堬細 杩欓噷鐨? 杩欓噷璧嬪€肩粰 鐜板湪锛岃祴鍊肩粰 閭d箞灏辨妸閲岄潰鐨勭浉搴旂殑鎶借薄鎸埌澶栭潰鏉ャ€傛暣涓畾涔夊氨鍙樻垚浜嗚繖鏍凤細 璋冪敤锛?/p> 鐪嬬湅缁撴灉锛屾病闂銆?/p> 鐜板湪鍙互鎰夊揩鐜╄€嶄簡锛?/p> 銆傘€傘€備笉鑳芥槸 鍒氬ソ锛岃瘯璇曟垜鍓嶉潰鑷埗鐨勯偅涓細 濡傛灉鏄? 馃ぃ馃ぃ馃ぃ馃ぃ锛堝樋鍢垮樋鍢垮樋鍢垮樋鍢库€︹€︹€︹€︼級 锛堜笉杩囪繖娌″暐澶т笉浜嗙殑锛屾垜鍙槸娌¤兘瀹炵幇灏佽濂界殑 鐜板湪鍐嶆敼鏀癸紝灏辫兘鎷兼帴瀹屾暣鐨勪唬鐮佷簡銆傛垜鐩存帴涓婄粨鏋滐細 涓轰粈涔堜笉鐢? 鍏跺疄杩欓噷鍘绘帀瀛楃涓查噷鐨勪咯 鎯宠鐪嬬湅杩欎釜鏂囦欢鐨勮瘽锛屽彲浠?strong>閫€鍑?Python REPL 鐒跺悗杩欐牱锛?/p> 鐒跺悗璁块棶 杩欐槸涓?HTTP 浠g悊锛屼竴鑸敤浜庤皟璇曘€傜敓浜т腑寤鸿浣跨敤 Httpd 鎴栬€?Nginx 鏉ュ仛杩欎釜浜嬨€?/p> 鏈枃涓€鍏辨湁涓や釜 以上是关于銆愇?婕旂畻鍦?PY銆戞墧鎺夋父鏍囧崱灏哄悗鐢?PY 鎷兼帴浠g爜的主要内容,如果未能解决你的问题,请参考以下文章 銆愬睆骞曞姞宸ュ厬鎹?鍩硅銆戞洸灞?iPad 鎵嬭〃 浼樺娍鍔犲伐.PY
鐨勮剼鏈紝鏃犺繑鍥炪€?/li>Note
涓嶇劧锛屽氨浼氬儚 Powershell 閭f牱锛屼竴涓嚱鏁扮珶鐒跺彲浠ユ湁澶氫釜杩斿洖浜嗭紝閭e氨澶悡浜轰簡銆?/strong>\'{}\\n{}\'.format(1,2)
\'{}\\n{}\'.format("\'yaya{}rara\'",".format(2)")
\'({}\\n{})\'.format("\'yaya{}rara\'",".format(2)")
\'{}\\n{}\'.format("(\'yaya{}rara\'",".format(2))")
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)")) # 鎶ラ敊
()
锛屼富瑕佽繕鏄柟鎷彿 []
鍜屾媺鎷斿搾 lambda
銆傦級鍫嗘暟
import
锛屼絾 PY 灏辨槸涓嶈銆傚綋鐒讹紝涔熷彲鑳芥槸杩樻湁鍒殑鎴戜笉鐭ラ亾鐨勫姙娉曘€備絾涓嶇鎬庢牱锛屾棦鐒跺姝わ紝鎴戝氨缁欏紩鍏ョ殑闅忔満閲嶆柊璧蜂釜鍚嶅瓧濂戒簡锛屽氨鍙?闅忔満
濂戒簡锛?/p>import random as 闅忔満
Note
import
銆?/strong>
鍏跺疄鎴戝緢涓嶅枩娆㈣繖绉嶈璁★細鎴戣寰椾竴琛屼唬鐮佽嚜甯︿簡鎵€鏈夐儴鍒嗕細姣旇緝濂斤紝鑰屼笉鏄畠鍙槸蹇呰宸ヤ綔鐨勪竴閮ㄥ垎銆傚鏋滃彧鏄竴閮ㄥ垎锛?strong>浣犺瀛╁瓙浠笂鍝効鎵惧埆鐨勯儴鍒嗗幓鍛紵瀛╁瓙浠氨瑕佷激蹇冧簡鍛€馃あ馃あ銆傘€傘€傘€?/p>[闅忔満.randint(222,333) for i in range(12)]
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>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)] )
)
(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
lambda
鐨勫啓娉曘€?/p>7
涓殢鏈烘暟鐨勫垪琛紝闅忔満鏁版槸 222
鍜?333
涔嬮棿鐨?int
鏁帮紙鍗虫暣鏁帮級 銆?/p>濂戒簡锛屾垜鐜板湪鎯宠鎼炲ソ鍑犱釜杩欐牱鐨勫垪琛紒
[
(\'鍟嗗1\',(200,300)),
(\'鍟嗗2\',(50,600)),
(\'鍟嗗3\',(80,100))
]
\'鍟嗗x\'
鎷艰捣鏉ュ氨琛屸€︹€?/p>杩欎笉澶燂紒鎴戣繕鎯宠姣忎釜鐨勫弬鏁拌繕閮戒笉涓€鏍凤紒鎴戜笉鎯宠嚜宸卞畾锛屽氨鎯崇敤闅忔満鏁帮紒
闅忔満.randint(x,y)
杩欎釜鍑芥暟浼犲叆闅忔満鐨勫弬鏁帮紝骞朵笖瑕佺‘淇濈涓€涓皬浜庣浜屼釜銆?/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))
(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>[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>[ (lambda rndlow,add: (rndlow,rndlow+add)) (闅忔満.randint(40,300), 闅忔満.randint(10,600) ) for will_not_use in range(13) ]
13
涓€?/p>[ (lambda rndlow,add:
(rndlow , rndlow + add)
) (闅忔満.randint(40, 300), 闅忔満.randint(10, 600) )
for will_not_use in range(13)
]
Note
for
琛ㄨ揪寮忓彟璧蜂竴琛屽苟涓€娆$缉杩涖€?/li>lambda
琛ㄨ揪寮忕殑璇濓紝琚皟鐢ㄥ綋鐒惰鍖呬笂鍦嗘嫭鍙蜂簡灏便€傚畾涔夊ご鍙互灏介噺绱ц窡鐫€鍓嶅渾鎷彿锛屼篃鍙互鎹㈣锛屾崲琛岀殑璇濓紝鐩稿浜庡墠鍦嗘嫭鍙锋墍鍦ㄥ眰绾э紝鍐嶅線鍚庣缉杩涗袱灞傦紝杩欎釜鍓嶆嫭鍙峰搴旂殑鍚庢嫭鍙蜂篃鍚屾牱濡傛缂╄繘骞朵笖搴斿綋鍙湪杩欐椂鍊欐墠鍙﹁捣涓€琛岋紝闄ら潪浣犺兘璁╁畠涓婁笅瀵归綈锛涜繑鍥炲€奸儴鍒嗙殑瀹氫箟锛屽彲浠ュ彟璧蜂竴琛岋紝濡傛灉鍙﹁捣涓€琛岋紝灏辫鐩稿 lambda
澶氬線鍚庣缉杩涗竴娆°€?/li>lambda
瀹氫箟鐨勫皬鎷彿鍦ㄥ唴瀹瑰彟璧蜂竴琛岀殑鏃跺€欎互澶栵紝涓€鍒囨嫭鍙凤紝搴斿綋鎯冲姙娉曠‘淇濊涔堟í鐫€鏄竴瀵硅涔堢珫鐫€鏄竴瀵广€傚鏋滄柊澧炴柊鐨勬牸寮忓寲瑙勫垯锛屼篃鏄湪纭繚杩欎竴鏉$殑鍓嶆彁涓嬭繘琛岀殑锛屾垨鑰呭氨鏄负浜嗘洿濂界‘淇濊繖涓€鏉¤€岃繘琛屻€?/li>lambda
瀹氫箟琚皟鐢ㄧ殑璇濓紝涓轰簡濂界湅锛屾渶濂界‘淇濆寘瑁瑰畾涔夌殑鍚庢嫭鍙峰拰鍙傛暟鍒楄〃涔嬮棿鏈夋墍闂撮殧锛涗絾涓轰簡鏂逛究浣犺皟璇曪紝鎵€浠ユ渶濂藉埆闂撮殧鍒颁笅涓€琛岋紝鍙敤绌烘牸闂撮殧灏卞ソ鎷夈€?/li>lambda
瀹氫箟琚祴鍊肩殑鏃跺€欏彲浠ヤ笉琚嫭鍙峰寘瑁广€傚鏋滆鍖呰9锛屽垯鎸夌収鍙傛暟鍒楄〃鐨勮鍒欐潵锛屽嵆鍚戝悗涓€灞傘€傚洜涓哄墠鎷彿涓嶈兘鍙﹁捣涓€琛屻€傘€傘€?/li>[ (
\'鍟嗗{}\'.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>13
鍛細(lambda cnt:
[ (
\'鍟嗗{}\'.format(i+1) ,
(lambda rndlow,add:
(rndlow , rndlow + add)
) (闅忔満.randint(40, 300), 闅忔満.randint(10, 600))
)
for i in range(cnt)
]
) (13)
(\'鍟嗗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)
]
[
鍚庨潰绱ц窡鐫€鏂扮敓鍒楄〃鐨勬瘡涓厓绱犵殑璁$畻閫昏緫锛岃繖涓绠楃殑鍙橀噺渚濇嵁灏辨槸 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>[ \' .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)
]
]
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>from functools import reduce as 姹囨€?
姹囨€?(
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)
]
] )
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
瀹氫箟鏄棤鍙傜殑銆傝涓嶈鏈変竴涓嬪憿锛熷搸锛岃繖涓晢瀹舵暟閲忔垜鎯冲湪杩欐帶鍒讹紒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)
]
]
] )
(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)
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>python -m http.server 2341
鑺傜偣IP:2341/shangjias.html
灏辫兘鐪嬪埌缁撴灉浜嗐€?/p>澶囧繕
import
锛?/p>import random as 闅忔満
from functools import reduce as 姹囨€?
鍒嗕韩娉ㄦ槑鏉ユ簮: https://segmentfault.com/a/1190000040317072