杩唬鍣ㄥ拰鐢熸垚鍣ㄧ殑鐢ㄦ硶

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了杩唬鍣ㄥ拰鐢熸垚鍣ㄧ殑鐢ㄦ硶相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/pycha' title='pycha'>pycha   range   format   class   绾跨▼   鐜板湪   sleep   浣跨敤   last   

棣栧厛鍦ㄤ簡瑙hВ鏋愬紡涔嬪墠锛屾垜浠厛鏉ョ湅涓€涓垪瀛愶細涓€涓垪琛紝鍏冪礌鏄?-9锛屽垪琛ㄤ腑鐨勬瘡涓€艰嚜澧?锛岃濡備綍瀹炵幇锛?/p>

鏂规硶涓€锛氶亶鍘嗗垪琛紝瀵瑰叾鍏冪礌杩涜鍔?鎿嶄綔鍚庢斁鍒颁竴涓柊鐨勫垪琛ㄤ腑

1 lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2 
3 for index, i in enumerate(lst):
4     lst[index] += 1
5 print(lst)

鏂规硶浜岋細閫氳繃map鍑芥暟鏉ュ疄鐜?/p>

a = map(lambda x:x+1, lst)
print(a)
for x in a:
    print(x)

鏂规硶涓夛細閫氳繃鍒楄〃瑙f瀽寮忥紝涓€琛屾悶瀹?/p>

1 lst2 = [x+1 for x in lst]
2 print(lst2)

 銆€銆€鏂规硶涓夊氨鏄垪琛ㄨВ鏋愬紡鐨勫啓娉曪紝杩斿洖涓€涓柊鐨勫垪琛ㄣ€?/p>

 

銆€銆€閭d箞浠€涔堟槸鐢熸垚鍣ㄥ憿锛熼€氳繃鍒楄〃瑙f瀽寮忔垜浠彲浠ュ彂鐜帮紝瀹冧細鐩存帴鍒涘缓涓€涓柊鐨勫垪琛紝杩欐牱涓嶅ソ鐨勫湴鏂瑰氨鏄崰鐢ㄥ唴瀛橈紝鎴戜滑鐭ラ亾鍐呭瓨鏄湁闄愮殑锛屽鏋滃垪琛ㄤ腑鐨勫厓绱犳湁鍑犵櫨涓囷紝鑰屾湁鏃跺€欏氨浠呬粎闇€瑕佷釜鍒殑鏁版嵁锛岄偅涔堝氨浼氬ぇ澶ф氮璐瑰唴瀛樼殑绌洪棿銆?/p>

銆€銆€鎵€浠ワ紝鎴戜滑鏄惁鍙互鎯充竴绉嶅姙娉曟潵瑙e喅杩欎釜闂鍛紵鍦≒ython涓紝鐢熸垚鍣ㄥ氨寰堝ソ鐨勮В鍐充簡杩欎釜闂銆傛垜浠彲浠ュ亣璁惧垪琛ㄤ腑鐨勫厓绱犺兘鍚﹂€氳繃鏌愮绠楁硶鏉ユ帹绠楀嚭鏉ュ憿锛熷湪闇€瑕佹煇涓暟鎹殑鏃跺€欓€氳繃璁$畻鏉ュ緱鍒拌繖涓暟鎹紝杩欐牱灏变笉浼氱洿鎺ョ敓鎴愪竴涓垪琛ㄦ潵瀛樺偍璁稿鏃犵敤鐨勬暟鎹簡銆傚湪python涓紝杩欑涓€杈瑰惊鐜竴杈硅绠楃殑鏈哄埗锛屽氨鏄敓鎴愬櫒銆?/p>

銆€銆€鐢熸垚鍣ㄦ槸杩唬鍣ㄧ殑涓€绉嶏紝浣跨敤yield杩斿洖鍊煎嚱鏁帮紝姣忔璋冪敤yield浼氭殏鍋滐紝浣跨敤next()鍑芥暟鍜宻end()鍑芥暟鍙互鎭㈠鐢熸垚鍣ㄧ户缁笅娆¤绠椼€傜敓鎴愬櫒涓嶅悓浜庝竴鑸殑鍑芥暟浼氫竴娆℃€ц繑鍥炴墍鏈夋暟鎹紝鐢熸垚鍣ㄤ竴娆″彧鑳戒骇鐢熶竴涓暟鎹紝杩欐牱灏变笉浼氬崰鐢ㄥぇ閲忕殑鍐呭瓨绌洪棿銆?/p>

銆€銆€閭d箞濡備綍鍘诲垱寤轰竴涓敓鎴愬櫒锛岃繖绉嶆柟寮忔瘮杈冪畝鍗曪紝灏辨槸灏嗗垪琛ㄨВ鏋愬紡鐨刐]鎹㈡垚()锛屽氨鍙互鍒涘缓涓€涓敓鎴愬櫒(generator)锛?/p>

1 generator_demo = (x for x in range(6))
2 print(generator_demo)
3 
4 杩愯缁撴灉锛?
5 <generator object <genexpr> at 0x0000000000C3E360>

銆€銆€浠庣粨鏋滃彲浠ョ湅鍑猴紝鐢熸垚鍣ㄨ繑鍥炵殑鏄竴涓猤enerator瀵硅薄銆傝€屽垪琛ㄨВ鏋愬紡杩斿洖涓€涓垪琛ㄣ€傞偅涔堝浣曞幓灏嗙敓鎴愬櫒涓殑鍏冪礌涓€涓竴涓墦鍗板嚭鏉ュ憿锛熻繖灏遍渶瑕乶ext()鍑芥暟鏉ヨ繘琛屾搷浣滐細

 1 generator_demo = (x for x in range(3))
 2 
 3 print(next(generator_demo))
 4 print(next(generator_demo))
 5 print(next(generator_demo))
 6 print(next(generator_demo))
 7 
 8 杩愯缁撴灉锛?
 9 0
10 1
11 2
12 Traceback (most recent call last):
13   File "C:/Users/wj/PycharmProjects/MxOnline/test.py", line 10, in <module>
14     print(next(generator_demo))
15 StopIteration

銆€銆€鍙互鐪嬪嚭锛岀敓鎴愬櫒淇濆瓨鐨勬槸绠楁硶锛岄€氳繃next鍙互涓€娆′竴娆¤绠楀畠鐨勫厓绱狅紝鐩村埌鏈€鍚庝竴涓厓绱犵殑鏃跺€欑户缁璶ext浼氭姏鍑篠topIteration鐨勯敊璇€傚湪鐢熶骇鐜涓紝鏄熀鏈笉浼氱敤next杩欎釜鏂规硶鐨勩€傚洜涓虹敓鎴愬櫒涔熸槸鍙凯浠e璞★紝鍙互閫氳繃for寰幆鍘昏凯浠e畠鐨勫€硷細

1 generator_demo = (x for x in range(3))
2 
3 for x in generator_demo:
4     print(x)

銆€銆€閫氳繃杩欑鏂规硶鏉ヨ凯浠e畠鐨勫€煎氨涓嶄細鎶涘嚭StopIteration鐨勯敊璇簡銆?/p>

銆€銆€杩欑鍒涘缓鐢熸垚鍣ㄧ殑鍐欐硶鍥虹劧绠€鍗曪紝浣嗘槸濡傛灉鏄竴涓畻娉曢€昏緫姣旇緝澶嶆潅鐨勬椂鍊欙紝灏变笉閫傚悎閫氳繃杩欑绠€鍗曠殑鍐欐硶鏉ュ垱寤虹敓鎴愬櫒浜嗭紝鎴戜滑鍙互閫氳繃鍑芥暟鐨勫舰寮忔潵鍒涘缓鐢熸垚鍣紝渚嬪钁楀悕鐨勬枑娉㈤偅濂戞暟鍒楋細

1 def fib(max):
2     n, a, b = 0, 0, 1
3     while n < max:
4         a, b = b, a+b
5         print(a)
6         n += 1
7 
8     return 鈥?/span>done鈥?/span>

銆€銆€鍙互鐪嬪嚭锛宖ib鍑芥暟瀹為檯涓婂氨鏄畾涔変簡鏂愭尝閭e鏁板垪鐨勮繍绠楄鍒欙紝浠庣涓€涓厓绱犲紑濮嬶紝鍘绘帹绠楀嚭鍚庨潰鐨勫厓绱狅紝杩欑閫昏緫灏遍潪甯告兂鐢熸垚鍣ㄣ€傞偅涔堟垜浠氨鍙互灏嗚繖涓嚱鏁板幓瀹氫箟鎴愪竴涓敓鎴愬櫒鍑芥暟锛岄€氳繃yield鍏抽敭瀛楁潵瀹氫箟锛?/p>

 1 def fib(max):
 2     n, a, b = 0, 0, 1
 3     while n < max:
 4         yield b
 5         a, b = b, a+b
 6         n += 1
 7 
 8 print(fib(10))
 9 
10 杩愯缁撴灉锛?
11 <generator object fib at 0x00000000011BE360>

銆€銆€浠庤繍琛岀粨鏋滃彲浠ョ湅鍑猴紝杩斿洖浜嗕竴涓猤enerator瀵硅薄锛岃繖灏辫鏄庡皢杩欎釜鍑芥暟瀹氫箟鎴愪簡涓€涓敓鎴愬櫒鍑芥暟銆傝繖閲岃涓€涓嬫櫘閫氬嚱鏁板拰鐢熸垚鍣ㄥ嚱鏁扮殑鎵ц娴佺▼锛屾櫘閫氬嚱鏁版槸椤哄簭鎵ц鐨勶紝閬囧埌return鍏抽敭瀛楀氨杩斿洖鍑芥暟鐨勫€奸€€鍑哄嚱鏁般€傝€岀敓鎴愬櫒鍑芥暟鏄瘡娆¤皟鐢╪ext鐨勬椂鍊欐墽琛岋紝閬囧埌yield鍏抽敭瀛楄繑鍥炵涓€涓厓绱狅紝鐒跺悗灏嗗嚱鏁版寕璧凤紝绛夊緟涓嬩竴娆″敜閱掔户缁绠楀悗闈㈢殑鍊硷紝涔熷氨鏄彇澶氬皯鐢ㄥ灏戯紝涓嶄細鍘诲崰鐢ㄥ唴瀛樼┖闂淬€?/p>

銆€銆€杩欎釜鐢熸垚鍣ㄥ嚱鏁颁篃鏄彲杩唬瀵硅薄锛屾墍浠ュ彲浠ラ€氳繃for寰幆鍘昏凯浠d粬鐨勫€硷紝鑰屼笉鐢ㄥ幓閫氳繃next鍘诲彇鍊笺€?/p>

銆€銆€鎴戜滑杩樺彲浠ラ€氳繃yield鏉ュ疄鐜板湪鍗曠嚎绋嬫ā寮忎笅瀹炵幇骞跺彂杩愮畻鐨勬晥鏋滐細

 1 import time
 2 
 3 def consumer(name):
 4     print(鈥?/span>鑰佸笀璇达細鍑嗗涓婅浜?/span>鈥?/span>.format(name))
 5     while True:
 6         lesson = yield
 7         print(鈥?/span>寮€濮媨浜嗭紝鑰佹墜鏉ヨ璇句簡锛?/span>鈥?/span>.format(lesson, name))
 8 
 9 
10 def producer():
11     name1 = consumer(鈥?/span>寮犺€佹墜鈥?/span>)
12     name2 = consumer(鈥?/span>鐜嬭€佹墜鈥?/span>)
13     name1.__next__()
14     name2.__next__()
15 
16     for x in range(4):
17         time.sleep(1)
18         print(鈥?/span>鍒颁簡涓ゅ悕鍚屽鈥?/span>)
19         name1.send(x)
20         name2.send(x)

銆€銆€浠庤繖涓緥瀛愮殑杩愯缁撴灉鍙互鐪嬪嚭锛宯ext鍜宻end鐨勫尯鍒紝next鏄敜閱掑嚱鏁帮紝杩斿洖涓€娆″€硷紝send鏄敜閱掑嚱鏁帮紝骞跺悜鐢熸垚鍣ㄥ唴閮▂ield浼犻€掍竴涓€硷紝骞舵敼鍙榶ield鐨勮繑鍥炲€笺€?/p>

 

銆€銆€閭d箞浠€涔堟槸杩唬鍣ㄥ憿锛熻凯浠e氨鏄惊鐜紝鏄寚鍦ㄦ纭殑鑼冨洿鍐呰繑鍥炴湡寰呯殑鏁版嵁浠ュ強鍦ㄨ秴鍑鸿寖鍥翠互鍚庢姏鍑篠topIteration鐨勯敊璇仠姝㈣凯浠c€?/p>

銆€銆€鍙互鐩存帴浣滅敤浜巉or寰幆鐨勬暟鎹被鍨嬫湁锛屽瓧绗︿覆銆佸垪琛ㄣ€佸厓缁勩€佸瓧鍏搞€侀泦鍚堢瓑锛岃繕鏈夌敓鎴愬櫒涔熷彲浠ヨ繘琛岃凯浠o紝杩欎簺鐩存帴浣滅敤浜巉or寰幆鐨勫璞¢兘鍙互鍙仛鍙凯浠e璞terable銆?/p>

銆€銆€涓€涓疄鐜颁簡iter鏂规硶鐨勫璞℃槸鍙凯浠g殑锛屼竴涓疄鐜颁簡next鏂规硶骞朵笖鏄彲杩唬鐨勫璞℃槸杩唬鍣ㄣ€?/p>

以上是关于杩唬鍣ㄥ拰鐢熸垚鍣ㄧ殑鐢ㄦ硶的主要内容,如果未能解决你的问题,请参考以下文章

axios鎷︽埅鍣ㄧ殑鐢ㄦ硶

鎷︽埅鍣ㄥ拰杩囨护鍣ㄧ殑鍖哄埆

鍑忓皯鏈嶅姟鍣ㄥ帇鍔沺hp鐢熸垚闈欐€亁ml鏂囦欢

銆怢eetCode鍒烽銆慡QL-Second Highest Salary 鍙婃墿灞曚互鍙奜racle涓殑鐢ㄦ硶

drogon鍏ラ棬鏁欑▼涓?鎺у埗鍣ㄥ拰鏁版嵁搴撳鎴风浣跨敤

MongDB 鐢ㄦ硶