Python基础教程

010_Python封包与解包

Python封包与解包

元组的自动封包与解包

Python中将多个值赋值给一个变量(如序列),称为封包;反之,将一个变量(序列)拆分成多个值时,称为解包。Python中自动封包(默认)会生成一个元组对象。

Python将多个对象自动封包成元组。

# 自动封包成一个元组对象
a=1,2,3,4
print(a)          # (1, 2, 3, 4)
print(type(a))    # <class 'tuple'>

# 函数返回一个对象
def getValue1():
    a = 1, 2, 3, 4
    return a

# 函数返多个对象(本质是一个对象,元组)
def getValue2():
    return 1, 2, 3, 4

v1=getValue1()
print(v1)        # (1, 2, 3, 4)
v2=getValue2()
print(v2)        # (1, 2, 3, 4)

Python解包,将元组内各元素拆分出来。获取关心的数据,舍去不关心的值。

# 自动封包成一个元组对象
atup=1,2,3,4
print(atup)          # (1, 2, 3, 4)
print(type(atup))    # <class 'tuple'>

a1,a2,_,_=atup       # 解包时需要等号左右数值个数对应
# 解包后的值
print(a1)            # 1
print(a2)            # 2
print(_)             # 4  对于不需要的值,可以用不常用的变量接收,表示丢弃

def getValue():
    return 1, 2, 3, 4    # 封包

x,y,_,_=getValue()       # 解包
print(x,y)               # 1 2

字典遍历与自动解包

adict={
    "name":"张三",
    "age":20,
    "score":95.5
}
# 遍历字典,解包每个元组
for k,v in adict.items():
    print(k,v)

程序运行结果

C:UsershccmaAnaconda3python.exe E:/wkp01/p00/test01/py001/t09.py
name 张三
age 20
score 95.5

Process finished with exit code 0

用星号“*”解包

# 自动封包成一个元组对象
atup=1,2,3,4
print(atup)          # (1, 2, 3, 4)

a1,a2,*ax=atup       # 解包时需要等号左右数值个数对应
# 解包后的值
print(a1)            # 1
print(a2)            # 2
print(ax)            # [3, 4]
print(type(ax))      # <class 'list'>

星号“*”解包对比

# 自动封包成一个元组对象
atup=1,2,3,4
atup2=(1,2,3,4)
a,b,*c=(1,2,3,4)
alist=[1,2,3,4,5]
# 元组自动封包与星号解包
print(atup)             # (1, 2, 3, 4)
print(*atup)            # 1 2 3 4
# 元组正常定义与星号解包
print(atup2)            # (1, 2, 3, 4)
print(*atup2)           # 1 2 3 4
# 元组部分星号解包对象是列表list
print(c)                # [3, 4]
print(type(c))          # <class 'list'>
# 列表的定义与星号解包
print(alist)            # [1, 2, 3, 4, 5]
print(*alist)           # 1 2 3 4 5

星号“*”解包与函数参数

def getValue(*args):
    return args*2          # 元组复制成2份

a=(1,2,3)
print(a)                   # (1, 2, 3)   元组对象
print(*a)                  # 1 2 3       元组的解包

print(getValue(*a))        # (1, 2, 3, 1, 2, 3)   传参数(三个对象):1, 2, 3
print(getValue(*(1,2)))    # (1, 2, 1, 2)         传参数(二个对象):1, 2
print(getValue(1,2))       # (1, 2, 1, 2)         传参数(二个对象):1, 2
print(getValue((1,2)))     # ((1, 2), (1, 2))     传参数(一个对象):(1, 2)

用双星号“**”解包

# 可以接收任意成对(k,v)参数
def getValue1(**kwargs):
    for k,v in kwargs.items():
        print(k,v)
getValue1(english=100,time=2)

print("------------1-----------------------")

# 定义字典对象
adict={
    "name":"张三",
    "age":20,
    "score":95.5
}
# 字典可以被解包成(k,v)参数
getValue1(**adict)

print("-------------2----------------------")

# 定义普通函数,参数是字典类型
def getValue2(vdict):
    for k,v in vdict.items():
        print(k,v)
# 只能传入字典类型,不能接收任意(k,v)参数
getValue2(adict)

运行结果

C:UsershccmaAnaconda3python.exe E:/wkp01/p00/test01/py001/t09.py
english 100
time 2
------------1-----------------------
name 张三
age 20
score 95.5
-------------2----------------------
name 张三
age 20
score 95.5

Process finished with exit code 0

字典的解包与合并

# 定义字典对象
adict1={
    "name":"张三",
    "age":20,
    "score":95.5
}

adict2={
    "english":100,
    "time":2
}
# 字典的解包与合并
adict3={
    "school":"五道口技术学院",
    **adict1,
    **adict2
}

print(adict3)
# {'school': '五道口技术学院', 'name': '张三', 'age': 20, 'score': 95.5, 'english': 100, 'time': 2}
这篇文章对您有用吗?

我们要如何帮助您?