当前位置: 代码迷 >> 综合 >> day08 列表的相关函数、深浅拷贝、字典相关函数、集合的相关操作和函数
  详细解决方案

day08 列表的相关函数、深浅拷贝、字典相关函数、集合的相关操作和函数

热度:70   发布时间:2024-01-29 05:05:47.0

列表的相关函数

append()

# 向列表的末尾添加新元素
lst = ["啦啦啦"]
lst.append(1)
print(lst)

insert

# insert 在指定索引之前插入元素
# lst.insert(指定索引,元素)
lst = ["啦啦啦"]
lst.insert(0,2)
print(lst)

lst.insert(0,2)
print(lst)

extend

# extend 迭代追加所有元素,向列表的末尾依次添加
# lst.extend(可迭代数据)
lst = ["啦啦啦"]
lst.extend("3456")
print(lst)

pop

lst = [1,2,3,4,5,6,7,8,9]
# pop 通过指定索引删除元素,若没有索引移除最后那个,返回值为删除元素
# 1.lst.pop() 移除最后那个元素
res = lst.pop()
print(lst)# 2.lst.pop(指定元素索引值)
res = lst.pop(3)
print(lst)
print(res)

remove

# remove 通过给予的值来删除,如果相同元素,默认删除第一个
lst = [1,2,6,3,4,5,6,7,8,9]
lst.remove(6)
print(lst)

clear

# 清空列表
lst = [1,2,6,3,4,5,6,7,8,9]
lst.clear()
print(lst)

index

# 获取某个值在列表中的索引
lst = [1,2,6,3,4,5,6,7,8,9]
# 列表.index(值)
print(lst.index(6))
# 列表.index(值,起始位置)
print(lst.index(6,3))
# 列表.index(值,起始位置,终止位置)
print(lst.index(6,3,6))

count

# 计算某个元素出现的次数
lst = [1,2,6,3,4,5,6,7,8,9]
print(lst.count(6))""" 区别:字符串里面的count 可以划定范围,列表里面的count不行 """

sort

# 列表排序(默认小到大排序)
lst = [44,99,1,10,3,-5,-90]
lst.sort()
print(lst)# 从大到小排序
lst.sort(reverse=True)
print(lst)# 排序英文 (ascii 编码)
# 一位一位进行比较,在第一位相同的情况下,比较第二位,以此类推
lst = ["oneal","kobe","james","jordan","macdi"]
lst.sort()
print( lst )# 是否可以对中文进行排序.可以排序!(无规律可循)
lst = ["啦啦","嘎嘎","哇哇","哈哈"]
lst.sort()
print(lst)

reverse

# 列表反转操作
lst = [1,2,6,3,4,5,6,7,8,9]
lst.reverse()
print(lst)

深浅拷贝

浅拷贝

# 1.浅拷贝
import copy
lst1 = [1,2,3]
# 方法一 copy.copy 模块.方法
lst2 = copy.copy(lst1)lst1.append(4)
print(lst1)
print(lst2)# 方法二 列表.copy()
lst1 = [1,2,3,4]
lst3 = lst1.copy()
lst1.insert(0,0)
print(lst1)
print(lst3)

深拷贝

lst1 = [1,2,3,[4,5,6]]
lst2 = copy.deepcopy(lst1)
# lst1[-1].extend("ab")
print(lst2)
print(lst1)print(id(lst2[-1]))
print(id(lst1[-1]))print(id(lst1[0]))
print(id(lst2[0]))lst2[0] = 1111
print(id(lst1[0]))
print(id(lst2[0]))

总结

1.浅拷贝只拷贝一级容器中的所有数据
2.深拷贝拷贝所有层级的所有元素,浅拷贝速度比深拷贝速度快
3.深拷贝在执行时: 如果是不可变数据,地址会暂时的指向原来数据,如果是可变数据,直接开辟新空间不可变数据: Number str tuple
可变数据  : list set dict

字典相关函数

fromkeys

# 使用一组键和默认值创建字典
lst = ["1","2","3"]
dic = {}.fromkeys(lst,None)
print(dic)# 注意点: 三个键所指向的列表是同一个(不推荐)

pop

# 通过键去删除键值对 (若没有该键可设置默认值,预防报错)
dic = {"1":"1" , "2":"2" , "3":"3" ,  "4" : "4" , "5":"5" }
res = dic.pop("5")
print(res , dic)
# 如果删除的是不存在的键,直接报错,可以设置默认值,防止报错
res = dic.pop("111","没有这个键")
print(res , dic)

popitem

# 删除最后一个键值对 
dic = {"1":"1" , "2":"2" , "3":"3" ,  "4" : "4" , "5":"5" }
res = dic.popitem()
print(res, dic)

clear

#clear() 清空字典
dic = {"1":"1" , "2":"2" , "3":"3" ,  "4" : "4" , "5":"5" }
dic.clear()
print(dic)

update

# 批量更新(有该键就更新,没该键就添加)
dic = {"1":"1" , "2":"2" , "3":"3" ,  "4" : "4" , "5":"5" }
dic_new = {"1": "11", "2": "22"}# 方法一(推荐)
dic.update(dic_new)
print(dic)# 方法二
dic.update(1 ="11",2="22")
print(dic)

get

# 通过键获取值(若没有该键可设置默认值,预防报错)
dic = {"1":"1" , "2":"2" , "3":"3" ,  "4" : "4" , "5":"5" }
res = dic.get("1")
print(res)
res = dic.get("11","没有这个键")
print(res)

keys

# 将字典的键组成新的可迭代对象
dic = {"1":"1" , "2":"2" , "3":"3" ,  "4" : "4" , "5":"5" }
res = dic.keys()
print(res)

values

# 将字典中的值组成新的可迭代对象
dic = {"1":"1" , "2":"2" , "3":"3" ,  "4" : "4" , "5":"5" }
res = dic.values()
print(res)

items

# 将字典的键值对凑成一个个元组,组成新的可迭代对象 
dic = {"1":"1" , "2":"2" , "3":"3" ,  "4" : "4" , "5":"5" }
res = dic.items()
print(res)

集合的相关操作和函数

intersection

# 交集 
set1 = {"1","2","3","4"}
set2 = {"1","5","6","2","7","8"}
print(set1.intersection(set2))# 简写 &
print(set1 & set2)

difference

# 差集 
set1 = {"1","2","3","4"}
set2 = {"1","5","6","2","7","8"}
print(set1.difference(set2))# 简写 -
print(set1 - set2)

union

# 并集 
set1 = {"1","2","3","4"}
set2 = {"1","5","6","2","7","8"}
print(set1.union(set2))# 简写 |
print(set1 | set2)

symmetric_difference

# 对称差集 (补集情况涵盖在其中) 
set1 = {"1","2","3","4"}
set2 = {"1","5","6","2","7","8"}
print(set1.symmetric_difference(set2))# 简写 ^
print(set1 ^ set2)

issubset

# 判断是否是子集
set1 = {"1","2","3","4"}
set2 = {"1","5","6","2","7","8"}
print(set1.issubset(set2))# 简写 < 
print(set1 < set2)

issuperset

# 判断是否是父集
set1 = {"1","2","3","4"}
set2 = {"1","5","6","2","7","8"}
print(set1.issuperset(set2))# 简写 > 
print(set1 > set2)

isdisjoint

# 检测两集合是否不相交 不相交 True 相交False
set1 = {"1","2","3","4"}
set2 = {"1","5","6","2","7","8"}
print(set1.isdisjoint(set2))

add

# 向集合中添加数据 (一次加一个)
set1 = {"1","2","3","4"}
set1.add("5")
print(setvar)

update

# 迭代着增加 (一次加一堆)
set1 = {"1","2","3","4"}
set2 = {"1","5","6","2","7","8"}
set1.update(set2)
print(setvar)

clear

# 清空集合
set1 = {"1","2","3","4"}
set1.clear()
print(setvar)

pop

# 随机删除集合中的一个数据
set1 = {"1","2","3","4"}
print(set,set1.pop())

remove

# 删除集合中指定的值(不存在则报错)
set1 = {"1","2","3","4"}
set1.remove("99")
print(set1)

discard

# 删除集合中指定的值(不存在的不删除)
set1 = {"1","2","3","4"}
set1.discard("4")
set1.discard("49")
print(set1)

frozenset

# 可强转容器类型数据变为冰冻集合
# 冰冻集合一旦创建,不能在进行任何修改,只能做交叉并补操作
lst1 = ["1","2","3","4"]
fz1 = frozenset(lst1)# 不能够在冰冻集合当中添加或者删除元素
# 只能交差并补