数据类型转换

 1 #数据类型转换
 2 
 3 # list -> set
 4 l1 = [1,2,3]
 5 s1 = set(l1)
 6 # set -> list
 7 l2 = list(s1)
 8 print(l1)
 9 print(l2)
10 print(s1)
11 # tuple -> set
12 t1 = (2,3,4)
13 s2 = set(t1)
14 # set -> tuple
15 t2 = tuple(s2)
16 print(t1)
17 print(t2)
18 print(s2)
19 
20 # string -> set
21 str1 = "abc"
22 s3 = set(str1)
23 print(s3)
24 # str2 = str(s3)
25 # print(str2)
26 
27 # dict -> set
28 d1 = {"a":1, "b":2}
29 s5 = set(d1)
30 print(s5)
31 # set -> dict 不能转换
32 # d4 = dict(s5)
33 # print(d4)

 

posted @ 2019-07-26 20:10  巨菜的小鸟  阅读(93)  评论(0编辑  收藏  举报