python學習筆記——列表

來源:互聯網
上載者:User

建立

>>> list = ["a", "b", "c", "d", "e"]>>> list['a', 'b', 'c', 'd', 'e']

擷取某一元素或子串

>>> list = ["a", "b", "c", "d", "e"]#取得第一個元素>>> list[0]'a'# 取得最後一個元素>>> list[-1]'e'# 若索引值超出列表長度之外,就會報錯>>> list[10]Traceback (most recent call last):  File "<stdin>", line 1, in <module>IndexError: list index out of range# 使用切片,類比javascript的slice功能,擷取子串>>> list[1:4]['b', 'c', 'd']# 切片的起始索引預設為0>>> list[:3]['a', 'b', 'c']# 切片的起始索引預設為-1>>> list[3:]['d', 'e']# 超出範圍會自動修正為列表的長度>>> list[3:10]['d', 'e']

替換

>>> list = ["a", "b", "c", "d", "e"]# 替換單個元素>>> list[3] = "D">>> list['a', 'b', 'c', 'D', 'e']# 替換N個元素>>> list[:3] = ["A", "B", "C"]>>> list['A', 'B', 'C', 'D', e]# 通換替換進行擴充,有點像javascript的splice>>> list[-1:] = ["E", "F"]>>> list['A', 'B', 'C', 'D', 'E', 'F']# 通換替換進行收縮>>> list[-2:] = "E">>> list['A', 'B', 'C', 'D', 'E']

追加

>>> list = ['b']

# push一個元素,注意此方法沒有傳回值
>>> list.append('e')
>>> list
>>> ['b', 'e']

# 在指定位置上添加元素
>>> list.insert(0, 'a')
>>> list
['a', 'b', 'e']

# 在指定位置上添加多個無形
>>> list[2:2] = ['c', 'd']
>>> list
['a', 'b', 'c', 'd', 'e']

合并

>>> list1 = ['a', 'b', 'c']>>> list2 = ['d', 'e']#  使用+號操作符產生一個全新的列表>>> list3 = list1 + list2>>> list3['a', 'b', 'c', 'd', 'e']>>> list1['a', 'b', 'c']# 使用extend在原列表上進行擴充,注意此方法沒有傳回值>>> list1.extend(list2)>>> list1['a', 'b', 'c', 'd', 'e']

檢測

>>> list = ["a", "b", "c", "a", "b"]# 判定其是否為目標列表的成員>>> "b" in listTrue>>> "e" in listFalse# 使用index方法還可以取得其在列表的位置>>> list.index("a")0# 但是找不到時會報錯,shit,什麼破設計>>> list.index("e")Traceback (most recent call last):  File "<stdin>", line 1, in ValueError: list.index(x): x not in list

刪除

>>> list = ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']# 類似javascript的pop函數>>> list.pop()'b'>>> list['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c']# 可以通過傳參,實現javascript的shift功能>>> list.pop(0)'a'>>> list['a', 'a', 'b', 'b', 'b', 'c', 'c']# 指定移除的元素,每次只移除1個>>> list.remove('b')>>> list['a', 'a', 'b', 'b', 'c', 'c']# 但如果不存在就會報錯,這個非常不人性化>>> list.remove(10)Traceback (most recent call last):  File "<stdin>", line 1, in ValueError: list.remove(x): x not in list# 清空所有‘b’元素>>> while 'b' in list: list.remove('b')...>>> list['a', 'a', 'c', 'c']# 移除一組元素>>> del list[1:3] #與 list[1:3] = [] 相同>>> list['a', 'c']# 全部清空>>> del list[:] #與 list[:] = [] 相同>>> list[]

統計

>>> list = ['a', 'a', 'b', 'c']>>> len(list)4# 返回a在列表出現的次數>>> list.count('a')2

列表解析

>>> S = [x**2 for x in range(10)]>>> V = [2**i for i in range(13)]>>> M = [x for x in S if x % 2 == 0]>>> >>> print S; print V; print M[0, 1, 4, 9, 16, 25, 36, 49, 64, 81][1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096][0, 4, 16, 36, 64]
>>> noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]>>> primes = [x for x in range(2, 50) if x not in noprimes]>>> print primes[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
>>> words = 'The quick brown fox jumps over the lazy dog'.split()>>> print words['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']>>> >>> stuff = [[w.upper(), w.lower(), len(w)] for w in words]>>> for i in stuff:...     print i... ['THE', 'the', 3]['QUICK', 'quick', 5]['BROWN', 'brown', 5]['FOX', 'fox', 3]['JUMPS', 'jumps', 5]['OVER', 'over', 4]['THE', 'the', 3]['LAZY', 'lazy', 4]['DOG', 'dog', 3]>>> >>> stuff = map(lambda w: [w.upper(), w.lower(), len(w)], words)>>> for i in stuff:...     print i... ['THE', 'the', 3]['QUICK', 'quick', 5]['BROWN', 'brown', 5]['FOX', 'fox', 3]['JUMPS', 'jumps', 5]['OVER', 'over', 4]['THE', 'the', 3]['LAZY', 'lazy', 4]['DOG', 'dog', 3]

將一個二維列表平坦化

>>> list = [[1, 3, 5], [2, 4]]>>> [flatten for inner in list for flatten in inner][1, 3, 5, 2, 4]
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.