python鍑芥暟--enumerate()鏂规硶,,鏍囩锛?a hre
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 threefor 寰幆浣跨敤 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 threepython鍑芥暟--enumerate()鏂规硶
鏍囩锛?a href='http://www.byrx.net/so/1/col' title='col'>col
鍘熸枃鍦板潃锛歨ttps://www.cnblogs.com/hanjiali/p/11589584.html
评论关闭