筆記帶有個(gè)人側(cè)重點(diǎn),不追求面面俱到。
15 數(shù)據(jù)結(jié)構(gòu)
出處: 菜鳥教程 - Python3 數(shù)據(jù)結(jié)構(gòu)
15.1 將列表當(dāng)作隊(duì)列使用
在列表的最后添加或者彈出元素速度快,然而在列表里插入或者從頭部彈出速度卻不快(因?yàn)樗衅渌脑囟嫉靡粋€(gè)一個(gè)地移動(dòng))。
15.2 遍歷技巧
遍歷字典:
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
... print(k, v)
...
gallahad the pure
robin the brave
遍歷列表:
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
... print(i, v)
...
0 tic
1 tac
2 toe
遍歷多個(gè)序列:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-691219.html
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
... print('What is your {0}? It is {1}.'.format(q, a))
...
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
反向遍歷:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-691219.html
>>> for i in reversed(range(1, 10, 2)):
... print(i)
...
9
7
5
3
1
到了這里,關(guān)于菜鳥教程《Python 3 教程》筆記(15):數(shù)據(jù)結(jié)構(gòu)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!