python中list的crud

# list常用的方法
list = [1, 2, 3, 4, 5, 5, 4, 5]

# 長度
print(len(list))

# 增
list.append(8)
print(list, 'appent')
list2 = [9,99,9,9,9]
list.extend(list2)
print(list)
# +可以同時添加多個
list.extend((list2 + list2))
print(list)
# 刪
list.pop()  # 默認刪除最後一位
print(list)
list.pop(3)  # 指定索引
print(list)
list.remove(8)  # 指定值刪除
print(list)
# 改
list.reverse()  # 數組反轉
print(list)

list.insert(0, '漢字')  # 根據索引替換值
# print(list)

# 查
print(list[0])

# 使用index方法查找值在list中下標,爲了避免報錯,需要先判斷是否在list中
if 5 in list:
	print(list.count(5))  # 求list中最大值
	print(list.index(5))

# any 與 some ; every與all
print(all(x == 1 for x in list))
print(any(x == 1 for x in list))


#拷貝
listCopy = list.copy()
print(listCopy == list) # 值相同 使用copy方法返回的是list的深拷貝
listCopy = list # 直接賦值操作,返回的是淺拷貝(原數組有修改,會影響listCopy)

以上。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章