python學習筆記——列表

weixin_34391854發表於2011-06-19

建立


>>> 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 list
True
>>> "e" in list
False

# 使用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]

相關文章