python鍑芥暟--enumerate()鏂规硶,,鏍囩锛?a hre


鏍囩锛?a href='http://www.byrx.net/so/1/col' title='col'>col

enumerate() 鏂规硶

鎻忚堪

enumerate() 鍑芥暟鐢ㄤ簬灏嗕竴涓彲閬嶅巻鐨勬暟鎹璞?濡傚垪琛ㄣ€佸厓缁勬垨瀛楃涓?缁勫悎涓轰竴涓储寮曞簭鍒楋紝鍚屾椂鍒楀嚭鏁版嵁鍜屾暟鎹笅鏍囷紝涓€鑸敤鍦?for 寰幆褰撲腑銆?/p>

Python 2.3. 浠ヤ笂鐗堟湰鍙敤锛?.6 娣诲姞 start 鍙傛暟銆?/p>

璇硶

浠ヤ笅鏄?enumerate() 鏂规硶鐨勮娉?

enumerate(sequence, [start=0])

鍙傛暟

sequence -- 涓€涓簭鍒椼€佽凯浠e櫒鎴栧叾浠栨敮鎸佽凯浠e璞°€?/li>start -- 涓嬫爣璧峰浣嶇疆銆?/li>

杩斿洖鍊?/h3>

杩斿洖 enumerate(鏋氫妇) 瀵硅薄銆?/p>


瀹炰緥

浠ヤ笅灞曠ず浜嗕娇鐢?enumerate() 鏂规硶鐨勫疄渚嬶細

>>>seasons = [鈥?span class="hl-string">Spring鈥?span class="hl-code">, 鈥?span class="hl-string">Summer鈥?span class="hl-code">, 鈥?span class="hl-string">Fall鈥?span class="hl-code">, 鈥?span class="hl-string">Winter鈥?span class="hl-brackets">] >>> list(enumerate(seasons)) [(0, 鈥?span class="hl-string">Spring鈥?span class="hl-brackets">), (1, 鈥?span class="hl-string">Summer鈥?span class="hl-brackets">), (2, 鈥?span class="hl-string">Fall鈥?span class="hl-brackets">), (3, 鈥?span class="hl-string">Winter鈥?span class="hl-brackets">)] >>> list(enumerate(seasons, start=1)) # 涓嬫爣浠?1 寮€濮?span class="hl-code"> [(1, 鈥?span class="hl-string">Spring鈥?span class="hl-brackets">), (2, 鈥?span class="hl-string">Summer鈥?span class="hl-brackets">), (3, 鈥?span class="hl-string">Fall鈥?span class="hl-brackets">), (4, 鈥?span class="hl-string">Winter鈥?span class="hl-brackets">)]

鏅€氱殑 for 寰幆

>>>i = 0 >>> seq = [鈥?span class="hl-string">one鈥?span class="hl-code">, 鈥?span class="hl-string">two鈥?span class="hl-code">, 鈥?span class="hl-string">three鈥?span class="hl-brackets">] >>> for element in seq: ... print i, seq[i] ... i +=1 ... 0 one 1 two 2 three

for 寰幆浣跨敤 enumerate

>>>seq = [鈥?span class="hl-string">one鈥?span class="hl-code">, 鈥?span class="hl-string">two鈥?span class="hl-code">, 鈥?span class="hl-string">three鈥?span class="hl-brackets">] >>> for i, element in enumerate(seq): ... print i, element ... 0 one 1 two 2 three

python鍑芥暟--enumerate()鏂规硶

鏍囩锛?a href='http://www.byrx.net/so/1/col' title='col'>col

鍘熸枃鍦板潃锛歨ttps://www.cnblogs.com/hanjiali/p/11589584.html

评论关闭