数据容器的通用操作
- 5 类数据容器都支持for循环遍历
- 列表、元组、字符串支持while循环,集合、字典不支持(无法下标索引)
- 数据容器的通用统计功能,len(容器),max(容器),min(容器)
my_list = [21, 25, 21, 23, 22, 20]
my_tuple = (1, "Hello", True, (4, 5, 6), (4, 5, 6))
my_str = "Hello World"
my_set = {"hello", "world", "Hello", "World", "hello", "world", "Hello", "World"}
my_dict = {"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99, "tang4": 88}
print(len(my_list))
print(len(my_tuple))
print(len(my_str))
print(len(my_set))
print(len(my_dict))
print("-----------------------------------")
print(max(my_list))
# print(max(my_tuple)) # TypeError: '>' not supported between instances of 'str' and 'int'
my_tuple = ("1", "Hello", "True")
print(max(my_tuple))
print(max(my_str))
print(max(my_set))
print(max(my_dict))
print("-----------------------------------")
my_tuple = ("1", "Hello", "True")
print(min(my_list))
print(min(my_tuple))
print(min(my_str))
print(min(my_set))
print(min(my_dict))
6
5
11
4
4 |
---|
25 |
True |
r |
world |
tang4 |
----------------------------------- |
20 |
1 |
Hello
tang1
数据容器的通用转换
my_list = [21, 25, 21, 23, 22, 20]
my_tuple = (1, "Hello", True, (4, 5, 6), (4, 5, 6))
my_str = "Hello World"
my_set = {"hello", "world", "Hello", "World", "hello", "world", "Hello", "World"}
my_dict = {"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99, "tang4": 88}
print(f"----------list()---------")
print(list(my_list))
print(list(my_tuple))
print(list(my_str))
print(list(my_set))
print(list(my_dict))
print()
print(f"----------tuple()---------")
print(tuple(my_list))
print(tuple(my_tuple))
print(tuple(my_str))
print(tuple(my_set))
print(tuple(my_dict))
print()
print(f"----------str()---------") # 本体带有双引号 "" 输出自动去掉了双引号
print(str(my_list))
print(str(my_tuple))
print(str(my_str))
print(str(my_set))
print(str(my_dict))
print()
print(f"----------set()---------")
print(set(my_list))
print(set(my_tuple))
print(set(my_str))
print(set(my_set))
print(set(my_dict))
----------list()---------
[21, 25, 21, 23, 22, 20]
[1, 'Hello', True, (4, 5, 6), (4, 5, 6)]
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
['hello', 'world', 'Hello', 'World']
['tang1', 'tang2', 'tang3', 'tang4']
----------tuple()---------
(21, 25, 21, 23, 22, 20)
(1, 'Hello', True, (4, 5, 6), (4, 5, 6))
('H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd')
('hello', 'world', 'Hello', 'World')
('tang1', 'tang2', 'tang3', 'tang4')
----------str()---------
[21, 25, 21, 23, 22, 20]
(1, 'Hello', True, (4, 5, 6), (4, 5, 6))
Hello World
{'hello', 'world', 'Hello', 'World'}
{'tang1': 96, 'tang2': 97, 'tang3': 98, 'tang4': 88}
----------set()---------
{20, 21, 22, 23, 25}
{1, 'Hello', (4, 5, 6)}
{' ', 'r', 'o', 'e', 'l', 'W', 'H', 'd'}
{'hello', 'world', 'Hello', 'World'}
{'tang2', 'tang1', 'tang3', 'tang4'}
数据容器的通用排序功能
- sorted(容器,[reverse=True]) 将给定的容器进行排序,不加reverse=True默认从小到大排序,reverse=True参数降序排列
- 排序的结果是列表对象
my_list = [21, 25, 21, 23, 22, 20]
my_tuple = ("1", "Hello", "True")
my_str = "Hello World"
my_set = {"hello", "world", "Hello", "World", "hello", "world", "Hello", "World"}
my_dict = {"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99, "tang4": 88}
print(sorted(my_list))
print(sorted(my_tuple))
print(sorted(my_str))
print(sorted(my_set))
print(sorted(my_dict))
print()
print(sorted(my_list,reverse=True))
print(sorted(my_tuple,reverse=True))
print(sorted(my_str,reverse=True))
print(sorted(my_set,reverse=True))
print(sorted(my_dict,reverse=True))
[20, 21, 21, 22, 23, 25]
['1', 'Hello', 'True']
[' ', 'H', 'W', 'd', 'e', 'l', 'l', 'l', 'o', 'o', 'r']
['Hello', 'World', 'hello', 'world']
['tang1', 'tang2', 'tang3', 'tang4']
[25, 23, 22, 21, 21, 20]
['True', 'Hello', '1']
['r', 'o', 'o', 'l', 'l', 'l', 'e', 'd', 'W', 'H', ' ']
['world', 'hello', 'World', 'Hello']
['tang4', 'tang3', 'tang2', 'tang1']
字符串的大小比较
字符大小进行比较就是基于数字的码值大小进行比较的
字符串是按位进行比较,也就是一位位进行对比,只要有一位大,那么整体就大
print(f"abd大于abc,结果是:{"abd" > "abc"}")
print(ord("a")) # 查询a的ascii码
print(chr(97)) # 查询ascii码值为97代表的字符
abd大于abc,结果是:True
97
a