博客目录
- 一.list 列表
- 1.list 单列表
- 2.嵌套列表
- 3.正序取数
- 4.倒序取数
- 5.list 常用函数
- 6.index 函数
- 7.修改元素值
- 8.insert 函数
- 9.append 函数
- 10.extend 函数
- 11.del 函数
- 12.pop 函数
- 13.remove 函数
- 14.删除所有指定元素
- 15.clear 函数
- 16.count 函数
- 17.len 函数
- 18.reverse 函数
- 19.enumerate 函数
- 20.range 函数
- 21.list 列表循环
- 22.偶数列表
- 23.栈
- 24.切片
- 二.tuple 元组
- 1.tuple 元组定义
- 2.tuple 单元组
- 3.元组嵌套
- 4.tuple 元组取值
- 5.index 函数
- 6.count 函数
- 7.len 函数
- 8.tuple 元组遍历
- 9.tuple 元组修改
- 三.字符串
- 1.字符串
- 2.下标取值
- 3.index 函数
- 4.replace 函数
- 5.split 函数
- 6.strip 函数
- 7.count 函数
- 8.len 函数
- 9.list 列表切片
- 10.tuple 元组切片
- 11.字符串切片
- 12.反向切片
- 13.比较大小
- 四.集合
- 1.定义集合
- 2.add 函数
- 3.remove 函数
- 4.pop 函数
- 5.clear 函数
- 6.intersection 函数
- 7.difference 函数
- 8.difference_update
- 9.union 函数
- 10.len 函数
- 11.排序
- 12.集合遍历
- 五.字典
- 1.定义字典
- 2.定义空字典
- 3.重复 Key 的字典
- 4.获取 value
- 5.嵌套字典
- 6.嵌套字典中获取数据
- 7.新增元素
- 8.更新元素
- 9.pop 函数
- 10.clear 函数
- 11.获取全部的 key
- 12.遍历字典
- 13.len 函数
- 14.是否在字典
- 六.堆
- 1.堆的定义
- 2.添加元素
- 3.列表转换为堆
- 4.删除最小值
- 5.添加新的元素值
- 6.添加比较
- 7.合并堆
- 8.最大 n 个
- 9.最小 n 个
- 10.定义大顶堆
- 11.判断堆
- 七.json
- 1.dumps 函数
- 2.loads
- 八.工具包
- 1.numpy
- 九.总结对比
- 1.数据容器对比
- 2.len 函数
- 3.max 函数
- 4.min 函数
- 5.list 函数
- 6.tuple 函数
- 7.str 函数
- 8.set 函数
- 9.排序
- 10.反序
一.list 列表
1.list 单列表
- 定义 list
- type 获取类型
- 嵌套列表定义与获取
- 获取下标对应的值
- 倒序取出
# 定义一个列表 list
my_list = ["itheima", "itcast", "python"]
# 定义空列表
new_list = []
print(my_list)
print(type(my_list))
my_list = ["itheima", 666, True]
print(my_list)
print(type(my_list))
定义列表:
#方法一:局部变量
judge=[]
#方法二:方法参数
candies:List[int]
#方法三:局部变量--main函数
matrix=[2,3,5,1,3]
#方法四:间接生成
matrix=[0 for i in range(4)]
print(matrix) //[0, 0, 0, 0]
#方法五:数组乘法
matrix=[0]*5
print(matrix) //[0, 0, 0, 0, 0]
2.嵌套列表
# 定义一个嵌套的列表
my_list = [[1, 2, 3], [4, 5, 6]]
print(my_list)
print(type(my_list))
3.正序取数
# 通过下标索引取出对应位置的数据
my_list = ["Tom", "Lily", "Rose"]
# 列表[下标索引], 从前向后从0开始,每次+1, 从后向前从-1开始,每次-1
print(my_list[0])
print(my_list[1])
print(my_list[2])
# 错误示范;通过下标索引取数据,一定不要超出范围
# print(my_list[3])
# 取出嵌套列表的元素
my_list = [[1, 2, 3], [4, 5, 6]]
print(my_list[1][1])
4.倒序取数
# 通过下标索引取出数据(倒序取出)
print(my_list[-1])
print(my_list[-2])
print(my_list[-3])
# 取出最后一个元素(应是:30)
num2 = mylist[-1]
print(f"从列表中取出来最后一个元素,应该是30,实际上是:{num2}")
5.list 常用函数
- index 获取元素的下标
- insert 插入一个元素
- append 追加一个元素
- extend 追加一个列表
- del 删除指定位置元素
- pop 删除指定位置元素
- remove 删除元素
- clear 清空列表
- count 统计个数
- len 列表长度
- reverse 反转列表
- enumerate 迭代返回下标和数据
6.index 函数
mylist = ["itcast", "itheima", "python"]
# 查找某元素在列表内的下标索引
index = mylist.index("itheima")
print(f"itheima在列表中的下标索引值是:{index}")
# 如果被查找的元素不存在,会报错
# index = mylist.index("hello")
# print(f"hello在列表中的下标索引值是:{index}")
7.修改元素值
mylist = ["itcast", "itheima", "python"]
# 修改特定下标索引的值
mylist[0] = "传智教育"
print(f"列表被修改元素值后,结果是:{mylist}")
8.insert 函数
# 在指定下标位置插入新元素
mylist.insert(1, "best")
print(f"列表插入元素后,结果是:{mylist}")
9.append 函数
# 4. 在列表的尾部追加单个新元素
mylist.append("黑马程序员")
print(f"列表在追加了元素后,结果是:{mylist}")
10.extend 函数
# 在列表的尾部追加一批新元素
mylist2 = [1, 2, 3]
mylist.extend(mylist2)
print(f"列表在追加了一个新的列表后,结果是:{mylist}")
11.del 函数
mylist = ["itcast", "itheima", "python"]
# del 列表[下标]
del mylist[2]
print(f"列表删除元素后结果是:{mylist}")
12.pop 函数
# 列表.pop(下标)
mylist = ["itcast", "itheima", "python"]
element = mylist.pop(2)
print(f"通过pop方法取出元素后列表内容:{mylist}, 取出的元素是:{element}")
13.remove 函数
# 删除某元素在列表中的第一个匹配项
mylist = ["itcast", "itheima", "itcast", "itheima", "python"]
mylist.remove("itheima")
print(f"通过remove方法移除元素后,列表的结果是:{mylist}")
14.删除所有指定元素
删除所有为itheima的元素:
可以使用列表推导式和条件语句来移除列表中所有值为"itheima"的元素。以下是示例代码:
my_list = ["itheima", "hello", "world", "itheima", "python"]
new_list = [x for x in my_list if x != "itheima"]
print(new_list)
在上面的代码中,我们首先定义了一个名为 my_list 的列表,其中包含了一些元素,其中包括了一些值为"itheima"的元素。然后,我们使用列表推导式和条件语句来创建一个新的列表 new_list,其中包含了所有值不为"itheima"的元素。最后,我们打印输出了新的列表 new_list。
运行上面的代码,输出结果为:
['hello', 'world', 'python']
可以看到,所有值为"itheima"的元素都被成功移除了。
15.clear 函数
# 清空列表
mylist.clear()
print(f"列表被清空了,结果是:{mylist}")
16.count 函数
# 9. 统计列表内某元素的数量
mylist = ["itcast", "itheima", "itcast", "itheima", "python"]
count = mylist.count("itheima")
print(f"列表中itheima的数量是:{count}")
17.len 函数
# 统计列表中全部的元素数量
mylist = ["itcast", "itheima", "itcast", "itheima", "python"]
count = len(mylist)
print(f"列表的元素数量总共有:{count}个")
18.reverse 函数
# 翻转list
mylist = ["itcast", "itheima", "itcast", "itheima", "python"]
mylist.reverse()
print(f"通过reverse方法翻转后,列表的结果是:{mylist}")
19.enumerate 函数
在 Python 中,enumerate
是一个内置函数,用于将一个可迭代对象(如列表、元组、字符串等)组合为一个索引序列,同时列出数据和数据下标,常用于 for 循环中。
enumerate
函数的语法如下:
enumerate(iterable, start=0)
其中,iterable
是要枚举的可迭代对象,start
是索引起始值,默认为 0。
以下是一个简单的示例,演示了如何使用enumerate
函数:
fruits = ['apple', 'banana', 'orange']
for index, fruit in enumerate(fruits):
print(index, fruit)
输出结果如下:
0 apple
1 banana
2 orange
在这个示例中,我们使用enumerate
函数将一个列表fruits
转换为一个索引序列。在每次迭代时,enumerate
函数会返回一个元组,其中第一个元素是数据的索引,第二个元素是数据本身。我们可以使用两个变量index
和fruit
来接收这个元组,并打印出来。
20.range 函数
#方法四:间接生成
matrix=[0 for i in range(4)]
print(matrix) //[0, 0, 0, 0]
left = 0
for index in range(len(nums)):
if nums[index] != val:
nums[left] = nums[index]
left += 1
return left
left = 0
for index in range(1, len(nums)):
if nums[index] != val:
nums[left] = nums[index]
left += 1
return left
range(len(digits)-1, -1, -1)
这是一个倒序遍历列表 digits
的常用方式,它生成一个逆序的整数序列,用于反向遍历列表。具体来说,它生成了一个从 len(digits)-1
开始,到 -1 结束(不包括 -1),步长为 -1 的整数序列。
因为 Python 列表的索引是从 0 开始,所以 len(digits)-1
代表 digits
列表的最后一个元素的索引,-1 代表列表的第一个元素的索引。因此,这个序列包含了 digits
列表中所有的元素,且按照从后往前的顺序遍历。
在加一问题的解法中,我们需要从 digits
列表的最后一个元素开始加 1,如果加 1 后产生了进位,则需要继续向前进位。这个逆序的整数序列正好满足这个需求。
21.list 列表循环
def list_while_func():
"""
使用while循环遍历列表的演示函数
:return: None
"""
mylist = ["传智教育", "黑马程序员", "Python"]
# 循环控制变量:通过下标索引来控制,默认0
# 每一次循环将下标苏姚
index = 0
while index < len(mylist):
print(mylist[index])
index += 1
# list_while_func()
def list_for_func():
"""
使用for循环遍历列表的演示函数
:return:
"""
mylist = ["传智教育", "黑马程序员", "Python"]
for e in mylist:
print(e)
list_for_func()
22.偶数列表
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_list = []
i = 0
while i < len(my_list):
if my_list[i] % 2 == 0:
new_list.append(my_list[i])
i += 1
print(f"偶数形成的新列表:{new_list}")
23.栈
stack = [] #定义栈
stack[-1] #取栈顶
24.切片
# 对list进行切片,从1开始,4结束,步长1
my_list = [0, 1, 2, 3, 4, 5, 6]
result1 = my_list[1:4] # 步长默认是1,所以可以省略不写
二.tuple 元组
1.tuple 元组定义
不可改变
# 定义元组
t1 = (1, "Hello", True)
t2 = ()
t3 = tuple()
print(f"t1的类型是:{type(t1)}, 内容是:{t1}")
print(f"t2的类型是:{type(t2)}, 内容是:{t2}")
print(f"t3的类型是:{type(t3)}, 内容是:{t3}")
2.tuple 单元组
# 定义单个元素的元素
t4 = ("hello", )
print(f"t4的类型是:{type(t4)}, t4的内容是:{t4}")
3.元组嵌套
# 元组的嵌套
t5 = ( (1, 2, 3), (4, 5, 6) )
print(f"t5的类型是:{type(t5)}, 内容是:{t5}")
4.tuple 元组取值
t5 = ( (1, 2, 3), (4, 5, 6) )
# 下标索引去取出内容
num = t5[1][2]
print(f"从嵌套元组中取出的数据是:{num}")
5.index 函数
# 元组的操作:index查找方法
t6 = ("传智教育", "黑马程序员", "Python")
index = t6.index("黑马程序员")
print(f"在元组t6中查找黑马程序员,的下标是:{index}")
6.count 函数
# 元组的操作:count统计方法
t7 = ("传智教育", "黑马程序员", "黑马程序员", "黑马程序员", "Python")
num = t7.count("黑马程序员")
print(f"在元组t7中统计黑马程序员的数量有:{num}个")
7.len 函数
# 元组的操作:len函数统计元组元素数量
t8 = ("传智教育", "黑马程序员", "黑马程序员", "黑马程序员", "Python")
num = len(t8)
print(f"t8元组中的元素有:{num}个")
8.tuple 元组遍历
# 元组的遍历:while
index = 0
while index < len(t8):
print(f"元组的元素有:{t8[index]}")
# 至关重要
index += 1
# 元组的遍历:for
for element in t8:
print(f"2元组的元素有:{element}")
9.tuple 元组修改
# 修改元组内容
# t8[0] = "itcast"
# 定义一个元组
t9 = (1, 2, ["itheima", "itcast"])
print(f"t9的内容是:{t9}")
t9[2][0] = "黑马程序员"
t9[2][1] = "传智教育"
print(f"t9的内容是:{t9}")
三.字符串
1.字符串
- 下标取值
- index 获取下标
- replace 替换
- split 分割
- strip 去除空格
- strip 去除指定字符
- count 统计个数
- len 计算长度
# 定义字符串
my_str = "itheima and itcast"
2.下标取值
# 通过下标索引取值
value = my_str[2]
value2 = my_str[-16]
print(f"从字符串{my_str}取下标为2的元素,。值是:{value},取下标为-16的元素。值是:{value2}")
3.index 函数
# index方法
value = my_str.index("and")
print(f"在字符串{my_str}中查找and,其起始下标是:{value}")
4.replace 函数
# replace方法
new_my_str = my_str.replace("it", "程序")
print(f"将字符串{my_str},进行替换后得到:{new_my_str}")
5.split 函数
# split方法
my_str = "hello python itheima itcast"
my_str_list = my_str.split(" ")
print(f"将字符串{my_str}进行split切分后得到:{my_str_list}, 类型是:{type(my_str_list)}")
6.strip 函数
# strip方法
my_str = " itheima and itcast "
new_my_str = my_str.strip() # 不传入参数,去除首尾空格
print(f"字符串{my_str}被strip后,结果:{new_my_str}")
# strip方法带参数
my_str = "12itheima and itcast21"
new_my_str = my_str.strip("12")
print(f"字符串{my_str}被strip('12')后,结果:{new_my_str}")
# 也可以把前后的12和21去掉
my_str = "12itheima and itcast21"
new_my_str = my_str.strip("231")
print(f"字符串{my_str}被strip('12')后,结果:{new_my_str}")
7.count 函数
# count统计字符串中某字符串的出现次数
my_str = "itheima and itcast"
count = my_str.count("it")
print(f"字符串{my_str}中it出现的次数是:{count}")
8.len 函数
# 统计字符串的长度, len()
num = len(my_str)
print(f"字符串{my_str}的长度是:{num}")
9.list 列表切片
# 对list进行切片,从1开始,4结束,步长1
my_list = [0, 1, 2, 3, 4, 5, 6]
result1 = my_list[1:4] # 步长默认是1,所以可以省略不写
print(f"结果1:{result1}")
10.tuple 元组切片
# 对tuple进行切片,从头开始,到最后结束,步长1
my_tuple = (0, 1, 2, 3, 4, 5, 6)
result2 = my_tuple[:] # 起始和结束不写表示从头到尾,步长为1可以省略
print(f"结果2:{result2}")
11.字符串切片
# 对str进行切片,从头开始,到最后结束,步长2
my_str = "01234567"
result3 = my_str[::2]
print(f"结果3:{result3}")
12.反向切片
# 对str进行切片,从头开始,到最后结束,步长-1
my_str = "01234567"
result4 = my_str[::-1] # 等同于将序列反转了
print(f"结果4:{result4}")
# 对列表进行切片,从3开始,到1结束,步长-1
my_list = [0, 1, 2, 3, 4, 5, 6]
result5 = my_list[3:1:-1]
print(f"结果5:{result5}")
# 对元组进行切片,从头开始,到尾结束,步长-2
my_tuple = (0, 1, 2, 3, 4, 5, 6)
result6 = my_tuple[::-2]
print(f"结果6:{result6}")
13.比较大小
# abc 比较 abd
print(f"abd大于abc,结果:{'abd' > 'abc'}")
# a 比较 ab
print(f"ab大于a,结果:{'ab' > 'a'}")
# a 比较 A
print(f"a 大于 A,结果:{'a' > 'A'}")
# key1 比较 key2
print(f"key2 > key1,结果:{'key2' > 'key1'}")
# abd大于abc,结果:True
# ab大于a,结果:True
# a 大于 A,结果:True
# key2 > key1,结果:True
四.集合
1.定义集合
# 定义集合
my_set = {"传智教育", "黑马程序员", "itheima", "传智教育", "黑马程序员", "itheima", "传智教育", "黑马程序员", "itheima"}
my_set_empty = set() # 定义空集合
print(f"my_set的内容是:{my_set}, 类型是:{type(my_set)}")
print(f"my_set_empty的内容是:{my_set_empty}, 类型是:{type(my_set_empty)}")
2.add 函数
# 添加新元素
my_set.add("Python")
my_set.add("传智教育") #
print(f"my_set添加元素后结果是:{my_set}")
3.remove 函数
# 移除元素
my_set.remove("黑马程序员")
print(f"my_set移除黑马程序员后,结果是:{my_set}")
4.pop 函数
# 随机取出一个元素
my_set = {"传智教育", "黑马程序员", "itheima"}
element = my_set.pop()
print(f"集合被取出元素是:{element}, 取出元素后:{my_set}")
5.clear 函数
# 清空集合, clear
my_set.clear()
print(f"集合被清空啦,结果是:{my_set}")
6.intersection 函数
两个集合的交集
nums1=[1, 2, 2, 1]
nums2=[2, 2]
set(nums1).intersection(nums2)
7.difference 函数
# 取2个集合的差集
set1 = {1, 2, 3}
set2 = {1, 5, 6}
set3 = set1.difference(set2)
print(f"取出差集后的结果是:{set3}")
print(f"取差集后,原有set1的内容:{set1}")
print(f"取差集后,原有set2的内容:{set2}")
8.difference_update
# 消除2个集合的差集
set1 = {1, 2, 3}
set2 = {1, 5, 6}
set1.difference_update(set2)
print(f"消除差集后,集合1结果:{set1}")
print(f"消除差集后,集合2结果:{set2}")
9.union 函数
# 2个集合合并为1个
set1 = {1, 2, 3}
set2 = {1, 5, 6}
set3 = set1.union(set2)
print(f"2集合合并结果:{set3}")
print(f"合并后集合1:{set1}")
print(f"合并后集合2:{set2}")
10.len 函数
# 统计集合元素数量len()
set1 = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5}
num = len(set1)
print(f"集合内的元素数量有:{num}个")
11.排序
去重倒序
nums = sorted(set(nums), reverse=True) # 去重并按降序排序
12.集合遍历
# 集合的遍历
# 集合不支持下标索引,不能用while循环
# 可以用for循环
set1 = {1, 2, 3, 4, 5}
for element in set1:
print(f"集合的元素有:{element}")
五.字典
1.定义字典
# 定义字典
my_dict1 = {"王力鸿": 99, "周杰轮": 88, "林俊节": 77}
2.定义空字典
# 定义空字典
my_dict2 = {}
my_dict3 = dict()
print(f"字典1的内容是:{my_dict1}, 类型:{type(my_dict1)}")
print(f"字典2的内容是:{my_dict2}, 类型:{type(my_dict2)}")
print(f"字典3的内容是:{my_dict3}, 类型:{type(my_dict3)}")
3.重复 Key 的字典
# 定义重复Key的字典
my_dict1 = {"王力鸿": 99, "王力鸿": 88, "林俊节": 77}
print(f"重复key的字典的内容是:{my_dict1}")
# 重复key的字典的内容是:{'王力鸿': 88, '林俊节': 77}
# 后面的会覆盖前面的,并不会报错
4.获取 value
# 从字典中基于Key获取Value
my_dict1 = {"王力鸿": 99, "周杰轮": 88, "林俊节": 77}
score = my_dict1["王力鸿"]
print(f"王力鸿的考试分数是:{score}")
score = my_dict1["周杰轮"]
print(f"周杰轮的考试分数是:{score}")
5.嵌套字典
# 定义嵌套字典
stu_score_dict = {
"王力鸿": {
"语文": 77,
"数学": 66,
"英语": 33
}, "周杰轮": {
"语文": 88,
"数学": 86,
"英语": 55
}, "林俊节": {
"语文": 99,
"数学": 96,
"英语": 66
}
}
print(f"学生的考试信息是:{stu_score_dict}")
6.嵌套字典中获取数据
# 从嵌套字典中获取数据
# 看一下周杰轮的语文信息
score = stu_score_dict["周杰轮"]["语文"]
print(f"周杰轮的语文分数是:{score}")
score = stu_score_dict["林俊节"]["英语"]
print(f"林俊节的英语分数是:{score}")
7.新增元素
my_dict = {"周杰轮": 99, "林俊节": 88, "张学油": 77}
# 新增元素
my_dict["张信哲"] = 66
print(f"字典经过新增元素后,结果:{my_dict}")
8.更新元素
# 更新元素
my_dict["周杰轮"] = 33
print(f"字典经过更新后,结果:{my_dict}")
9.pop 函数
# 删除元素
score = my_dict.pop("周杰轮")
print(f"字典中被移除了一个元素,结果:{my_dict}, 周杰轮的考试分数是:{score}")
10.clear 函数
# 清空元素, clear
my_dict.clear()
print(f"字典被清空了,内容是:{my_dict}")
11.获取全部的 key
# 获取全部的key
my_dict = {"周杰轮": 99, "林俊节": 88, "张学油": 77}
keys = my_dict.keys()
print(f"字典的全部keys是:{keys}")
12.遍历字典
# 遍历字典
# 方式1:通过获取到全部的key来完成遍历
for key in keys:
print(f"字典的key是:{key}")
print(f"字典的value是:{my_dict[key]}")
# 方式2:直接对字典进行for循环,每一次循环都是直接得到key
for key in my_dict:
print(f"2字典的key是:{key}")
print(f"2字典的value是:{my_dict[key]}")
13.len 函数
# 统计字典内的元素数量, len()函数
num = len(my_dict)
print(f"字典中的元素数量有:{num}个")
14.是否在字典
num_dict = {}
for i, num in enumerate(nums):
# 关键判断 是否在字典中
if num in num_dict and i - num_dict.get(num) <= k:
return True
else:
num_dict[num] = i
return False
是否存在:
if j in dict1 and len(dict1[j]) > 0:
# 字典的值不为空
# 在这里编写相关代码
else:
# 字典的值为空
# 在这里编写相关代码
六.堆
1.堆的定义
heapq 库中的堆默认是最小堆,python 的 heapq 模块提供了对堆的支持
2.添加元素
heapq.heappush(heap, item) #往堆添加元素
3.列表转换为堆
heapq.heapify(list) #将列表转换为堆
4.删除最小值
heapq.heappop(heap) #删除并返回最小值 heap[0]永远是最小的元素
5.添加新的元素值
heapq.heapreplace(heap.item) #删除并返回最小元素值,添加新的元素值
6.添加比较
# 判断添加元素值与堆的第一个元素值对比;
# 如果大,则删除并返回第一个元素,然后添加新元素值item. 如果小,则返回item. 原堆不变。
heapq.heappushpop(list, item)
7.合并堆
heapq.merge(…) #将多个堆合并
8.最大 n 个
heapq.nlargest(n,heap) #查询堆中的最大n个元素
9.最小 n 个
heapq.nsmallest(n,heap) #查询堆中的最小n个元素
10.定义大顶堆
//默认小顶堆,改为大顶堆
heap = [-stone for stone in stones] #相当于数字取反,最后取出的时候要注意转换
heapq.heapify(heap)
11.判断堆
if heap: return -heap[0] #判断堆
七.json
1.dumps 函数
字典转 json 字符串
# 准备列表,列表内每一个元素都是字典,将其转换为JSON
data = [{"name": "张大山", "age": 11}, {"name": "王大锤", "age": 13}, {"name": "赵小虎", "age": 16}]
json_str = json.dumps(data, ensure_ascii=False)
print(type(json_str))
print(json_str)
# 准备字典,将字典转换为JSON
d = {"name": "周杰轮", "addr": "台北"}
json_str = json.dumps(d, ensure_ascii=False)
print(type(json_str))
print(json_str)
2.loads
字符串转字典
# 将JSON字符串转换为Python数据类型[{k: v, k: v}, {k: v, k: v}]
s = '[{"name": "张大山", "age": 11}, {"name": "王大锤", "age": 13}, {"name": "赵小虎", "age": 16}]'
l = json.loads(s)
print(type(l))
print(l)
# 将JSON字符串转换为Python数据类型{k: v, k: v}
s = '{"name": "周杰轮", "addr": "台北"}'
d = json.loads(s)
print(type(d))
print(d)
八.工具包
1.numpy
import numpy as np
class Solution:
def matrixSum(self, nums: List[List[int]]) -> int:
for i in nums:
i[:] = sorted(i)
return int(sum(np.max(np.array(nums), axis=0)))
九.总结对比
1.数据容器对比
2.len 函数
my_list = [1, 2, 3, 4, 5]
my_tuple = (1, 2, 3, 4, 5)
my_str = "abcdefg"
my_set = {1, 2, 3, 4, 5}
my_dict = {"key1": 1, "key2": 2, "key3": 3, "key4": 4, "key5": 5}
# len元素个数
print(f"列表 元素个数有:{len(my_list)}")
print(f"元组 元素个数有:{len(my_tuple)}")
print(f"字符串元素个数有:{len(my_str)}")
print(f"集合 元素个数有:{len(my_set)}")
print(f"字典 元素个数有:{len(my_dict)}")
3.max 函数
my_list = [1, 2, 3, 4, 5]
my_tuple = (1, 2, 3, 4, 5)
my_str = "abcdefg"
my_set = {1, 2, 3, 4, 5}
my_dict = {"key1": 1, "key2": 2, "key3": 3, "key4": 4, "key5": 5}
# max最大元素
print(f"列表 最大的元素是:{max(my_list)}")
print(f"元组 最大的元素是:{max(my_tuple)}")
print(f"字符串最大的元素是:{max(my_str)}")
print(f"集合 最大的元素是:{max(my_set)}")
print(f"字典 最大的元素是:{max(my_dict)}")
4.min 函数
my_list = [1, 2, 3, 4, 5]
my_tuple = (1, 2, 3, 4, 5)
my_str = "abcdefg"
my_set = {1, 2, 3, 4, 5}
my_dict = {"key1": 1, "key2": 2, "key3": 3, "key4": 4, "key5": 5}
# min最小元素
print(f"列表 最小的元素是:{min(my_list)}")
print(f"元组 最小的元素是:{min(my_tuple)}")
print(f"字符串最小的元素是:{min(my_str)}")
print(f"集合 最小的元素是:{min(my_set)}")
print(f"字典 最小的元素是:{min(my_dict)}")
5.list 函数
my_list = [1, 2, 3, 4, 5]
my_tuple = (1, 2, 3, 4, 5)
my_str = "abcdefg"
my_set = {1, 2, 3, 4, 5}
my_dict = {"key1": 1, "key2": 2, "key3": 3, "key4": 4, "key5": 5}
# 类型转换: 容器转列表
print(f"列表转列表的结果是:{list(my_list)}")
print(f"元组转列表的结果是:{list(my_tuple)}")
print(f"字符串转列表结果是:{list(my_str)}")
print(f"集合转列表的结果是:{list(my_set)}")
print(f"字典转列表的结果是:{list(my_dict)}")
6.tuple 函数
my_list = [1, 2, 3, 4, 5]
my_tuple = (1, 2, 3, 4, 5)
my_str = "abcdefg"
my_set = {1, 2, 3, 4, 5}
my_dict = {"key1": 1, "key2": 2, "key3": 3, "key4": 4, "key5": 5}
# 类型转换: 容器转元组
print(f"列表转元组的结果是:{tuple(my_list)}")
print(f"元组转元组的结果是:{tuple(my_tuple)}")
print(f"字符串转元组结果是:{tuple(my_str)}")
print(f"集合转元组的结果是:{tuple(my_set)}")
print(f"字典转元组的结果是:{tuple(my_dict)}")
7.str 函数
my_list = [1, 2, 3, 4, 5]
my_tuple = (1, 2, 3, 4, 5)
my_str = "abcdefg"
my_set = {1, 2, 3, 4, 5}
my_dict = {"key1": 1, "key2": 2, "key3": 3, "key4": 4, "key5": 5}
# 类型转换: 容器转字符串
print(f"列表转字符串的结果是:{str(my_list)}")
print(f"元组转字符串的结果是:{str(my_tuple)}")
print(f"字符串转字符串结果是:{str(my_str)}")
print(f"集合转字符串的结果是:{str(my_set)}")
print(f"字典转字符串的结果是:{str(my_dict)}")
8.set 函数
my_list = [1, 2, 3, 4, 5]
my_tuple = (1, 2, 3, 4, 5)
my_str = "abcdefg"
my_set = {1, 2, 3, 4, 5}
my_dict = {"key1": 1, "key2": 2, "key3": 3, "key4": 4, "key5": 5}
# 类型转换: 容器转集合
print(f"列表转集合的结果是:{set(my_list)}")
print(f"元组转集合的结果是:{set(my_tuple)}")
print(f"字符串转集合结果是:{set(my_str)}")
print(f"集合转集合的结果是:{set(my_set)}")
print(f"字典转集合的结果是:{set(my_dict)}")
9.排序
my_list = [1, 2, 3, 4, 5]
my_tuple = (1, 2, 3, 4, 5)
my_str = "abcdefg"
my_set = {1, 2, 3, 4, 5}
my_dict = {"key1": 1, "key2": 2, "key3": 3, "key4": 4, "key5": 5}
# 进行容器的排序
my_list = [3, 1, 2, 5, 4]
my_tuple = (3, 1, 2, 5, 4)
my_str = "bdcefga"
my_set = {3, 1, 2, 5, 4}
my_dict = {"key3": 1, "key1": 2, "key2": 3, "key5": 4, "key4": 5}
print(f"列表对象的排序结果:{sorted(my_list)}")
print(f"元组对象的排序结果:{sorted(my_tuple)}")
print(f"字符串对象的排序结果:{sorted(my_str)}")
print(f"集合对象的排序结果:{sorted(my_set)}")
print(f"字典对象的排序结果:{sorted(my_dict)}")
基础排序
nums.sort()
切片
for i in nums:
i[:] = sorted(i)
基于指定值排序
# 准备列表
my_list = [["a", 33], ["b", 55], ["c", 11]]
# 排序,基于带名函数
def choose_sort_key(element):
return element[1]
my_list.sort(key=choose_sort_key, reverse=True)
# 排序,基于lambda匿名函数
my_list.sort(key=lambda element: element[1], reverse=True)
print(my_list)
10.反序
my_list = [1, 2, 3, 4, 5]
my_tuple = (1, 2, 3, 4, 5)
my_str = "abcdefg"
my_set = {1, 2, 3, 4, 5}
my_dict = {"key1": 1, "key2": 2, "key3": 3, "key4": 4, "key5": 5}
# 进行容器的排序
my_list = [3, 1, 2, 5, 4]
my_tuple = (3, 1, 2, 5, 4)
my_str = "bdcefga"
my_set = {3, 1, 2, 5, 4}
my_dict = {"key3": 1, "key1": 2, "key2": 3, "key5": 4, "key4": 5}
print(f"列表对象的反向排序结果:{sorted(my_list, reverse=True)}")
print(f"元组对象的反向排序结果:{sorted(my_tuple, reverse=True)}")
print(f"字符串对象反向的排序结果:{sorted(my_str, reverse=True)}")
print(f"集合对象的反向排序结果:{sorted(my_set, reverse=True)}")
print(f"字典对象的反向排序结果:{sorted(my_dict, reverse=True)}")
觉得有用的话点个赞 👍🏻
呗。
❤️❤️❤️本人水平有限,如有纰漏,欢迎各位大佬评论批评指正!😄😄😄
💘💘💘如果觉得这篇文对你有帮助的话,也请给个点赞、收藏下吧,非常感谢!👍 👍 👍
🔥🔥🔥Stay Hungry Stay Foolish 道阻且长,行则将至,让我们一起加油吧!🌙🌙🌙