list1 = [1, 2, 3, 4, 5, 6, 6, 6]
tuple1 = (11, 22, 33, 44, 55, 66)
set1 = {'a', 'b', 'c', 'd', 'e'}
# 将列表以及集合转换成元组
list1_2_tuple = tuple(list1)
set1_2_tuple = tuple(set1)
print(list1_2_tuple)
print(set1_2_tuple) # 因为集合是无序的,所以当转换成元组时,其输出与原来可能不同
# 将元组以及集合转换成列表
tuple1_2_list = list(tuple1)
set1_2_list = list(set1)
print(tuple1_2_list)
print(set1_2_list) # 集合还有唯一性,具有去重功能,能快速完成列表去重,同一集合中输出的元素不能相同
# 将列表以及元组转换成集合
print(set(list1))
print(set(tuple1))
运行结果如下所示:
(1, 2, 3, 4, 5, 6, 6, 6)
('d', 'a', 'e', 'c', 'b')
[11, 22, 33, 44, 55, 66]
['d', 'a', 'e', 'c', 'b']
{1, 2, 3, 4, 5, 6}
{33, 66, 11, 44, 22, 55}
Process finished with exit code 0