元组除了用作不可变的列表之外,还可以用于没有字段名的记录
元组其实是对数据进行记录,元组中每个元素都存放了记录一个字段的数据和这个字段的位置,这个位置信息给数据赋予了意义,当你只把理解一个不可变的序列,那位置这个概念就变的可有可无了,但是当你理解成一些字段的集合,那位置就变的非常重要了
把元组用作记录
# 存城市的坐标
longitude_and_latitude = (30.236999, 118.225659)
# 存城市的年份、人口和面积
city, particular_year, population, area = ('北京', 3000, 2151.6, 16410.54)
# 列表中每个元组存一个城市名和面积
citys = [('Beijing', 16410.54), ('Tianjin', 11966.45), ('Shanghai', 6340.5), ('Chongqing', 80402)]
# 循环列表,拿出每个城市的数据并打印出来
for i in sorted(citys):
print('%s: %s' % i) # 两个占位符,进行元组拆包
运行后的结果
F:\project_gitee\Test>D:/Python/Python37/python.exe f:/project_gitee/Test/pythonScripts/python_tuple.py
Beijing: 16410.54
Chongqing: 80402
Shanghai: 6340.5
Tianjin: 11966.45
拆包元组可以完美地被当作记录来使用
被可迭代的元素数量必须和接收空档数量保持一致,不然就会拆包不成功
栗子:
正确拆包栗子:
用两个变量去接收,实现了拆包效果
citys = [('Beijing', 16410.54), ('Tianjin', 11966.45), ('Shanghai', 6340.5), ('Chongqing', 80402)]
for i in sorted(citys):
var, values = i
print(var, values)
运行后的结果
F:\project_gitee\Test>D:/Python/Python37/python.exe f:/project_gitee/Test/pythonScripts/python_tuple.py
Beijing: 16410.54
Chongqing: 80402
Shanghai: 6340.5
Tianjin: 11966.45
错误拆包栗子:
如果你用一个变量来接收,接收就是一个元组,是赋值的操作,实现不了拆包数据
citys = [('Beijing', 16410.54), ('Tianjin', 11966.45), ('Shanghai', 6340.5), ('Chongqing', 80402)]
for i in sorted(citys):
var = i
print(var)
运行后的结果
F:\project_gitee\Test>D:/Python/Python37/python.exe f:/project_gitee/Test/pythonScripts/python_tuple.py
('Beijing', 16410.54)
('Chongqing', 80402)
('Shanghai', 6340.5)
('Tianjin', 11966.45)
元组中有几个值,就用几个变量进行接收
city, particular_year, population, area = ('北京', 3000, 2151.6, 16410.54)
运行后的结果
F:\project_gitee\Test>D:/Python/Python37/python.exe f:/project_gitee/Test/pythonScripts/python_tuple.py
北京 3000 2151.6 16410.54
小知识点:平行赋值交换参数,不用使用中间变量接收
a, b = (30.236999, 118.225659)
print(a, b)
b, a = a, b
print(a, b)
参数交换后的运行结果
F:\project_gitee\Test>D:/Python/Python37/python.exe f:/project_gitee/Test/pythonScripts/python_tuple.py
30.236999 118.225659
118.225659 30.236999
通常运用在函数/方法中居多
栗子:
longitude_and_latitude = (30.236999, 118.225659)
print(*longitude_and_latitude)
运行后的结果
F:\project_gitee\Test>D:/Python/Python37/python.exe f:/project_gitee/Test/pythonScripts/python_tuple.py
30.236999 118.225659
栗子:
def tuple_nupack(*args):
print(*args)
tuple_nupack(longitude_and_latitude)
运行后的结果
F:\project_gitee\Test>D:/Python/Python37/python.exe f:/project_gitee/Test/pythonScripts/python_tuple.py
(30.236999, 118.225659)
编码过程中,我们可能会忽略一些不想要的数据,可以使用“ _ ” 占位
栗子:
_, d = (30.236999, 118.225659)
print(d)
运行后的结果
F:\project_gitee\Test>D:/Python/Python37/python.exe f:/project_gitee/Test/pythonScripts/python_tuple.py
118.225659
针对处理一些多余数据的接收,也可以使用 “ * ” 符号
args是获取不确定数量参数,是python中大家都遵循的写法,当然你也可以换成你喜欢的
栗子:
e, f, *args = range(5)
print(e, f, args)
运行后的结果
F:\project_gitee\Test>D:/Python/Python37/python.exe f:/project_gitee/Test/pythonScripts/python_tuple.py
0 1 [2, 3, 4]
平行赋值中, * 前缀只能用在一个变量名前,但是这个变量可以出现在赋值表达式的任意位置
栗子:
e, *body, f = range(5)
print(e, f, body)
运行后的结果
F:\project_gitee\Test>D:/Python/Python37/python.exe f:/project_gitee/Test/pythonScripts/python_tuple.py
0 4 [1, 2, 3]
给元组构建一个带字段名的元组:工厂函数collections中的namedtuple
栗子:
from collections import namedtuple
City = namedtuple('City', ('city particular_year population area')) # 1
beijing = City('北京', 3000, 2151.6, 16410.54) # 2
print(beijing) # 3
print(beijing.particular_year) # 4
print(beijing.population) # 5
运行后的结果
F:\project_gitee\Test>D:/Python/Python37/python.exe f:/project_gitee/Test/pythonScripts/python_tuple.py
City(city='北京', particular_year=3000, population=2151.6, area=16410.54)
3000
2151.6
具名元组的属性和方法
栗子:
from collections import namedtuple
City = namedtuple('City', ('city particular_year population area'))
print(City._fields) # 1
city_handle = City._make(('南京', 3000, 2151.6, 16410.54)) # 2
print(city_handle)
print(city_handle._asdict()) # 3
运行后的结果
F:\project_gitee\Test>D:/Python/Python37/python.exe f:/project_gitee/Test/pythonScripts/python_tuple.py
('city', 'particular_year', 'population', 'area')
City(city='南京', particular_year=3000, population=2151.6, area=16410.54)
OrderedDict([('city', '南京'), ('particular_year', 3000), ('population', 2151.6), ('area', 16410.54)])
栗子:
g = (1, 2, 3)
print(g.index(1))
print(g.count(2))
运行后的结果
F:\project_gitee\Test>D:/Python/Python37/python.exe f:/project_gitee/Test/pythonScripts/python_tuple.py
0
1
元组还有一些特殊方法,在此就不再罗列了......