Python/ target=_blank class=infotextkey>Python中列表是最常用的數據類型之一,由多個元素組成的集合,每個元素都有一個位置或者叫索引,索引的值從0開始,往后順序遞推,最大值為列表長度-1
例如
aa = [1, 2, 3, 4, 5]
print(aa[0]) # 1
print(aa[1]) # 2
print(aa[2]) # 3
常用方法
索引、切片
# 索引 切片
l = ['a', 'b', 'c', 'd', 'e', 'f']
# 優先掌握部分
print(l[1:5])
print(l[1:5:2])
print(l[2:5])
print(l[-1])
"""
['b', 'c', 'd', 'e']
['b', 'd']
['c', 'd', 'e']
f
"""
# 了解
print(l[-1:-4])
print(l[-4:-1])
print(l[-4:])
l = ['a', 'b', 'c', 'd', 'e', 'f']
print(l[-2:])
"""
[]
['c', 'd', 'e']
['c', 'd', 'e', 'f']
['e', 'f']
"""
追加元素Append()
hobbies = ['play', 'eat', 'sleep', 'study']
hobbies.append('girls')
print(hobbies) # ['play', 'eat', 'sleep', 'study', 'girls']
刪除元素
pop()
如果pop()里面沒加參數 則默認刪除最后一個元素
hobbies = ['play', 'eat', 'sleep', 'study', 'run', "girl"]
x = hobbies.pop(1) # 不是單純的刪除,是刪除并且把刪除的元素返回,我們可以用一個變量名去接收該返回值
print(x)
print(hobbies)
x = hobbies.pop(0)
print(x)
x = hobbies.pop(0)
print(x)
x = hobbies.pop()
print(x)
"""
eat
['play', 'sleep', 'study', 'run', 'girl']
play
sleep
girl
"""
del
del和pop() 不一樣, 他沒有返回值,只是單純的將參數里面索引對應的元素從列表里面刪除
這種刪除方式不光在列表中有用,在后面的元組和字典里也是有用的
hobbies = ['play', 'eat', 'sleep', 'study']
del hobbies[1] # 單純的刪除
print(hobbies) # ['play', 'sleep', 'study']
remove()
remove()參數是具體的元素值,不是索引,也沒有返回值
hobbies = ['play', 'eat', 'sleep', 'study']
hobbies.remove('eat') # 單純的刪除,并且是指定元素去刪除
print(hobbies) # ['play', 'sleep', 'study']
用append()和pop()隊列和堆棧
隊列:是一種數據結構,其特點是先進先出,就和排隊一樣,排在最前面的人優先買到東西
堆棧: 是一種數據結構,其特點是后進先出,就和往桶里面放東西一樣,最后放進去的,往往是最先拿出來
# 隊列:先進先出
queue_l = []
# 入隊
queue_l.append('first')
queue_l.append('second')
queue_l.append('third')
print(queue_l) # ['first', 'second', 'third']
# 出隊
print(queue_l.pop(0)) # first
print(queue_l.pop(0)) # second
print(queue_l.pop(0)) # third
# 堆棧:先進后出,后進先出
l = []
# #入棧
l.append('first')
l.append('second')
l.append('third')
# #出棧
print(l) # ['first', 'second', 'third']
print(l.pop()) # third
print(l.pop()) # second
print(l.pop()) # first
計算列表的長度
hobbies=['play','eat','sleep','study']
print(len(hobbies)) # 4
列表是否包含某個元素
對于字符串來說也是可以的
# 包含in
hobbies = ['play', 'eat', 'sleep', 'study']
print('sleep' in hobbies) # True
msg = 'hello world sz'
print('sz' in msg) # True
插入元素insert()
hobbies = ['play', 'eat', 'sleep', 'study', 'eat', 'eat']
hobbies.insert(1, 'walk')
print(hobbies)
hobbies.insert(1, ['walk1', 'walk2', 'walk3'])
print(hobbies)
"""
['play', 'walk', 'eat', 'sleep', 'study', 'eat', 'eat']
['play', ['walk1', 'walk2', 'walk3'], 'walk', 'eat', 'sleep', 'study', 'eat', 'eat']
"""
統計個數count()
hobbies = ['play', 'eat', 'sleep', 'study', 'eat', 'eat']
print(hobbies.count('eat')) # 3
列表合并extend()
list1.extend(list2) 將list2中的元素從list1的末尾添加上list1中
hobbies = ['play', 'eat', 'sleep', 'study', 'eat', 'eat']
hobbies.extend(['walk1', 'walk2', 'walk3'])
print(hobbies)
# ['play', 'eat', 'sleep', 'study', 'eat', 'eat', 'walk1', 'walk2', 'walk3']
獲取某個元素的索引index()
如果列表中有當前元素,則返回當前元素第一次出現的索引
如果列表中沒有當前元素,沒有就報錯
hobbies = ['play', 'eat', 'sleep', 'study', 'eat', 'eat']
print(hobbies.index('eat')) # 1
print(hobbies.index('girl')) # 報錯
清空元素clear()
hobbies = ['play', 'eat', 'sleep', 'study', 'eat', 'eat']
hobbies.clear()
print(hobbies) # []
列表拷貝copy()
列表拷貝屬于淺拷貝, 修改列表里面的元素會相互影響,切記,這里不展開說,后面會詳細說
hobbies = ['play', 'eat', 'sleep', 'study', 'eat', 'eat']
l = hobbies.copy() # ['play', 'eat', 'sleep', 'study', 'eat', 'eat']
print(l)
列表反轉reverse()
l = [1, 2, 3, 4, 5]
l.reverse()
print(l) # [5, 4, 3, 2, 1]
列表排序sort()
l = [100, 9, -2, 11, 32]
l.sort()
print(l) # [-2, 9, 11, 32, 100]
l = [1, 4, 5, 2, 6]
l.sort(reverse=True)
print(l) # [6, 5, 4, 2, 1]