亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

維護計數器變量的Python方式?

維護計數器變量的Python方式?

皈依舞 2021-03-31 13:14:01
我有這種代碼:count = 0for line in lines:    #do something with line    #do something more with line    #finish doing that thing with line    count = count + 1    if count % 10000 == 0:        print count這是在python中維護計數變量的正確方法嗎?我可以使它看起來更好嗎?
查看完整描述

1 回答

?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

您可以使用enumerate():


for count, line in enumerate(lines):

    #do something here

enumerate()也接受可選的第二個參數start,您可以使用該參數指定的起始值count。默認值為start0。


幫助enumerate:


>>> help(enumerate)


 |  enumerate(iterable[, start]) -> iterator for index, value of iterable

 |  

 |  Return an enumerate object.  iterable must be another object that supports

 |  iteration.  The enumerate object yields pairs containing a count (from

 |  start, which defaults to zero) and a value yielded by the iterable argument.

 |  enumerate is useful for obtaining an indexed list:

 |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ...


查看完整回答
反對 回復 2021-04-05
  • 1 回答
  • 0 關注
  • 230 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號