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)

以上。

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