Python3-筆記-C-003-函數-enumerate

來源:互聯網
上載者:User

標籤:廣州   python   ros   test   lin   筆記   mil   檔案   pre   

def testenumerate():
# enumerate(iterable, start=0)
# enumerate將iterable組成一個索引序列,利用它可以同時獲得索引和值
# 多用於在for迴圈中得到計數

l = [‘a‘, ‘b‘, ‘c‘] # <class ‘list‘>: [‘a‘, ‘b‘, ‘c‘]
l1 = list(enumerate(l)) # <class ‘list‘>: [(0, ‘a‘), (1, ‘b‘), (2, ‘c‘)]
t = (‘快樂‘, ‘高興‘, ‘開心‘) # <class ‘tuple‘>: (‘快樂‘, ‘高興‘, ‘開心‘)
l1 = list(enumerate(t)) # <class ‘list‘>: [(0, ‘快樂‘), (1, ‘高興‘), (2, ‘開心‘)]
d = {‘深圳‘: 1, ‘廣州‘: 2, ‘珠海‘: 3}
l1 = list(enumerate(d, 2)) # <class ‘list‘>: [(2, ‘深圳‘), (3, ‘廣州‘), (4, ‘珠海‘)]
s = ‘深圳1800萬人‘
l1 = list(enumerate(s, 1)) # <class ‘list‘>: [(1, ‘深‘), (2, ‘圳‘), (3, ‘1‘), (4, ‘8‘), (5, ‘0‘), (6, ‘0‘), (7, ‘萬‘), (8, ‘人‘)]

# 遍曆
list1 = ["這", "是", "一個", "測試"]
for index, item in enumerate(list1, 1):
print(index, item)
# 1 這
# 2 是
# 3 一個
# 4 測試

# 統計檔案的行數
count = 0
for index, line in enumerate(open("file.txt", ‘r‘)):
count += 1
print(count)

Python3-筆記-C-003-函數-enumerate

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.