元组 ,列表, 集合间的转换

  • 列表元组集合间隔转换的函数
原始类型 目标函数 函数 举例
列表 集合 set new_set = set([1, 2, 3, 4, 5])
列表 元组 tuple new_tutple = tuple([1, 2, 3, 4, 5])
元组 集合 set new_set = set((1, 2, 3, 4, 5))
元组 列表 list new_list = list(1, 2, 3, 4, 5)
集合 列表 list new_list = list({1, 2, 3, 4, 5})
集合 元组 tuple new_tuple = tuple({1, 2, 3, 4, 5})

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# coding:utf-8

a = [1, 2, 3]
b = (1, 2, 3)
c = {1, 2, 3}

print(tuple(a), set(a))
print(type(tuple(a)), type(set(a)))
print(tuple(a) is b)
print(set(a) == c)

print(list(b), set(b))
print(list(c), tuple(c))

print(list(a))

print(str(a), type(str(a))) # '[1, 2, 3]'
print(str(b), type(str(b)))
print(str(c), type(str(c)))

# 转换不可逆...
print(list(str(a)))
print(tuple(str(b)))
print(set(str(c)))