python元组简单介绍
时间:2022-04-15 09:30:06|栏目:Python代码|点击: 次
元组的特点:是一种不可变序列,一旦创建就不能修改
1、拆包
将元组的元素取出赋值给不同变量
>>> a = ('hello', 'world', 1, 2, 3)
>>> str1, str2, n1, n2, n3 = a
>>> str1
'hello'
>>> str2
'world'
>>> n1
1
>>> n2
2
>>> n3
3
>>> str1, str2, *n = a
>>> str1
'hello'
>>> str2
'world'
>>> n
[1, 2, 3]
>>> str1, _, n1, n2, _ = a
2、enumerate
解释:用于元组遍历,获得元组对象,第一个元素是索引,第二个是数值
a = ('1', 2, 35, 'hello')
for i in enumerate(a):
print(i)
>>> (0, '1')
>>> (1, 2)
>>> (2, 35)
>>> (3, 'hello')
3、list()
元组转换成列表
Python
a = ('1', 2, 35, 'hello')
print(list(a))
>>> ['1', 2, 35, 'hello']
上一篇:python通过Matplotlib绘制常见的几种图形(推荐)
栏 目:Python代码
本文标题:python元组简单介绍
本文地址:http://www.codeinn.net/misctech/199186.html






