[Python] 实现tuple和list的转换

文章展示了如何在Python中将list转换为tuple以及将tuple转换为list。通过使用tuple()函数将list强制转换为元组,而使用list()函数则可将元组转换为列表。这些基本操作是Python数据结构操作的重要部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.list列表转换为tuple元组

temp_list = [1,2,3,4,5]
print(temp_list)  # [1, 2, 3, 4, 5]
print(type(temp_list))  # <class 'list'>
# 将temp_list进行强制转换
a = tuple(temp_list)    
print(a)  # (1, 2, 3, 4, 5)
print(type(a))  # <class 'tuple'>

2.tuple元组转换为list列表

temp_tuple = (1,2,3,4)
print(temp_tuple)  # (1, 2, 3, 4)
print(type(temp_tuple))  # <class 'tuple'>
# 将temp_tuple进行强制转换
b = list(temp_tuple)    
print(b)  # [1, 2, 3, 4]
print(type(b))  # <class 'list'>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值