=> python入门篇07-数据容器(序列 集合 字典,json初识)基础(下)
数据容器)小总结介绍了几种数据容器 以及相对应的api操作
应用场景以及基础小案例
数据容器总结进阶| 数据容器 | list列表 [] | tuple元组() | str字符串"" | set集合{} | dict字典{k:v} |
|---|---|---|---|---|---|
| 下标索引 | ✓ | ✓ | ✓ | ✗ | ✗ |
| 重复元素 | ✓ | ✓ | ✓ | ✖ | ✖ |
| 可否修改 | ✓ | ✖ | ✖ | ✓ | ✓ |
| 是否有序 | ✓ | ✓ | ✓ | ✖ | ✖ |
| while循环 | ✓ | ✓ | ✓ | ✖ | ✖ |
| for循环 | ✓ | ✓ | ✓ | ✓ | ✓ |
准备
测试案例代码
list1 = [1, 2, 3]
tuple1 = (1, 2, 3, 4, 5)
str1 = "abcdefg"
set1 = {1, 2, 3, 4, 5}
dict1 = {"key1": 1, "key2": 2}
len() 方法print(len(list1))
print(len(tuple1))
print(len(str1))
print(len(set1))
print(len(dict1))
max() 方法print(max(list1))
print(max(tuple1))
print(max(str1))
print(max(set1))
print(max(dict1))
min() 方法print(min(list1))
print(min(tuple1))
print(min(str1))
print(min(set1))
print(min(dict1))
list() 方法print(list(list1)) # [1, 2, 3]
print(list(tuple1)) # [1, 2, 3, 4, 5]
print(list(str1)) # ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(list(set1)) # [1, 2, 3, 4, 5]
print(list(dict1)) # ['key1', 'key2']
tuple() 方法print(tuple(list1)) # (1, 2, 3)
print(tuple(tuple1)) # (1, 2, 3, 4, 5)
print(tuple(str1)) # ('a', 'b', 'c', 'd', 'e', 'f', 'g')
print(tuple(set1)) # (1, 2, 3, 4, 5)
print(tuple(dict1)) # ('key1', 'key2')
set() 方法print(set(list1)) # {1, 2, 3}
print(set(tuple1)) # {1, 2, 3, 4, 5}
print(set(str1)) # {'g', 'a', 'f', 'b', 'e', 'd', 'c'}
print(set(set1)) # {1, 2, 3, 4, 5}
print(set(dict1)) # {'key1', 'key2'}
sorted() 方法print(sorted(list1)) # [1, 2, 3]
print(sorted(tuple1)) # [1, 2, 3, 4, 5]
print(sorted(str1)) # ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(sorted(set1)) # [1, 2, 3, 4, 5]
print(sorted(dict1)) # ['key1', 'key2']
# 倒序 reverse默认是 false 正序
print(sorted(dict1, reverse=True)) # ['key2', 'key1']
ASCII编码)与ASCII 数字对应的 American Standard Code for Information Interchange: American信息交换标准代码
# abd 与 abc比较
print('abd' > 'abc') # True
# a 与 A 比较
print('a' > 'A') # True
# ba 大于 ab 先比较第一位 然后再比较第二位 9897>9798
print('ba' > 'ab') # True
def test_func():
return 1, 2, 3
# 看一种报错
# arg1, arg2 = test_func()
# print(arg1) # ValueError: too many values to unpack (expected 2)
arg1, arg2, arg3 = test_func()
print(arg1) # 1
print(arg2) # 2
print(arg3) # 3
print(test_func()) # (1, 2, 3) 元组
def test_func01(name, age, address):
print(f"名字:{name},年龄:{age},地址:{address}")
参数位置传递test_func01("张三", 13, "地球")
关键字参数传递(这种类似 k-v结构 位置就
可以不对应了)
test_func01(name="张三", address="地球", age=13)
ps: 看一个
报错
TypeError: test_func01() missing 1 required positional argument: ‘age’
# test_func01(name="张三", address="地球")
缺省参数传递给参数设置默认值 不传递也可以
注意设置默认值的多个参数 都必须放到最后
test_func01(name="张三", address="地球", age=13)
测试代码+整体效果如下:
# 名字:张三,年龄:13,地址:地球
# 名字:张三,年龄:13,地址:地球
# 名字:张三,年龄:13,地址:火星
# 名字:张三,年龄:13,地址:中国
test_func02(age=13, name="张三")
test_func02("张三", 13)
test_func02(age=13, address="火星", name="张三")
test_func02("张三", 13, "中国")
不定长参数传递不定长参数(
类似java的可变参数)
不定长定义的形式参数作为元组存在 用 “*” 表示
def test_func03(*args):
print(args) # (1, 2, 3, 4)
print(type(args)) #
test_func03(1, 2, 3, 4)
关键字不定长参数传递变成 字典 用 “
**” 表示
java没有此写法 直接传递map
def test_func04(**kwargs):
print(kwargs) # {'age': 12, 'name': '张三'}
print(type(kwargs)) #
test_func04(age=12, name="张三")
这个有点意思, 也就是把
动作行为装成参数进行传递了
参数传递的是函数 参数名随便写 也就是将
函数运算过程(不是参数) 传递进去
def test_func01(test_func):
result = test_func(1, 2)
print(type(test_func)) #
print(result) # 3
def test_func02(arg1, arg2):
return arg1 + arg2
test_func01(test_func02) # 3
对上面函数进行改造 (
一行代码lambda,多行使用def)
test_func01(lambda x, y: x + y)
python入门篇09- 文件操作,函数, 包及模块的综合案例

函数的不同传递方式, 使用更加灵活
作者pingzhuyan 感谢观看