大家好,又见面了,我是你们的朋友全栈君。
元组的特点:是一种不可变序列,一旦创建就不能修改
将元组的元素取出赋值给不同变量
>>> 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
解释:用于元组遍历,获得元组对象,第一个元素是索引,第二个是数值
a = ('1', 2, 35, 'hello')
for i in enumerate(a):
print(i)
>>> (0, '1')
>>> (1, 2)
>>> (2, 35)
>>> (3, 'hello')
元组转换成列表
a = ('1', 2, 35, 'hello')
print(list(a))
>>> ['1', 2, 35, 'hello']
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/165086.html原文链接:https://javaforall.cn
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有