操作層面分為取出資料、如何走訪和如何異動資料
可以取出list中的某一個資料和部分資料,分別為indexed
和sliced
這兩種操作都可以使用正向和反向的順序取出list,正向從0開始,反向從-1開始,超過索引範圍的話會丟出IndexError
,索引值不是整數或slice表示法會丟出TypeError
。
sliced會回傳新的list,範圍是包含起始但不包含結尾
All slice operations return a new list containing the requested elements. This means that the following slice returns a new (shallow) copy of the list
demo = [x for x in range(10)]
print( "demo data:{}".format(demo) )
# demo data:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# indexed
print( "first data:{}".format(demo[0]) )
print( "last data:{}".format(demo[-1]) )
# first data:0
# last data:9
try:
print(demo[10])
except IndexError as e:
print( "Index Error: {0}".format(e) )
# Index Error: list index out of range
try:
print(demo["10"])
except TypeError as e:
print( "Type Error: {0}".format(e) )
# Type Error: list indices must be integers or slices, not str
# sliced
s1 = demo[:]
print( "sliced 1:{}".format(s1) )
s2 = demo[2:]
print( "sliced 2:{}".format(s2) )
s3 = demo[2:5]
print( "sliced 3:{}".format(s3) )
s4 = demo[:5]
print( "sliced 4:{}".format(s4) )
s5 = demo[-3:]
print( "sliced 5:{}".format(s5) )
s6 = demo[::2]
print( "sliced 6:{}".format(s6) )
s7 = demo[1::2]
print( "sliced 7:{}".format(s7) )
# sliced 1:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# sliced 2:[2, 3, 4, 5, 6, 7, 8, 9]
# sliced 3:[2, 3, 4]
# sliced 4:[0, 1, 2, 3, 4]
# sliced 5:[7, 8, 9]
# sliced 6:[0, 2, 4, 6, 8]
# sliced 7:[1, 3, 5, 7, 9]
使用for
加上in
或是while
走訪list
demo = [x for x in range(10)]
print( "demo data:{}".format(demo) )
# demo data:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# iteration
summay = 0
for number in demo:
print(number)
summay += number
print( "summay:{}".format(summay) )
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# summay:45
summay2 = 0
idx = 0
while idx < len(demo):
print(demo[idx])
summay2 += demo[idx]
idx += 1
print( "summay2:{}".format(summay2) )
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# summay2:45
demo = [x for x in range(10)]
print( "demo data:{}".format(demo) )
# demo data:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# modification
demo.append(11)
print( "demo data after append:{}".format(demo) )
demo.insert(-1 ,10)
print( "demo data after insert:{}".format(demo) )
demo.extend( list(range(12, 21)) )
print( "demo data after extend:{}".format(demo) )
demo.extend( [1, 20, 5, 20] )
demo.remove(20)
demo.remove(5)
print( "demo data after remove:{}".format(demo) )
demo[1:5] = [9, 9, 9, 9]
print( "demo data after slice replace:{}".format(demo) )
demo[2:6] = []
print( "demo data after slice remove:{}".format(demo) )
# demo data after append:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]
# demo data after insert:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
# demo data after extend:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
# demo data after remove:[0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1, 20, 5, 20]
# demo data after slice replace:[0, 9, 9, 9, 9, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1, 20, 5, 20]
# demo data after slice remove:[0, 9, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1, 20, 5, 20]