干货!Python中字符串、列表、元祖、字典,集合之间的转换

一、字符串的转化

1、字符串转换成列表

字符串转换成list 的时候,str可以作为迭代对象,直接放入;也可以使用split对字符串进行切割。然后返回list

1 s = '1a1b1c'
2 print(list(s))
3 print(s.split('1'))
1 ['1', 'a', '1', 'b', '1', 'c']
2 ['', 'a', 'b', 'c']

2、字符串转化成元祖

1 s = '1a1b1c'
2 print(tuple(s))
1 ('1', 'a', '1', 'b', '1', 'c')

3、字符串转换成字典

1 a='a1b1c1'
2 b='123456'
3 print(zip(a,b))
4 print(dict(zip(a,b)))
1 <zip object at 0x00000231AF32D1C8>
2 {'a': '1', '1': '6', 'b': '3', 'c': '5'}
1 a='a1b2c3d4e5'
2 b='123456'
3 print(zip(a,b))
4 print(dict(zip(a,b)))
5 <zip object at 0x000002745544D208>
6 {'a': '1', '1': '2', 'b': '3', '2': '4', 'c': '5', '3': '6'}

4、字符串转换成列表

注重去重

1 a='a1b1c1'
2 print(set(a))
1 {'c', '1', 'b', 'a'}

二、列表的转化

1、列表转换成字符串

注:列表里如果有int类型,字典,元祖,列表,则join方法不可用

1 list = ["1","a","bb"]
2 print("".join(list))
3 print(" ".join(list))
4 print("!".join(list))
5 1abb
6 1 a bb
7 1!a!bb

2、列表转换成元祖

1 list = ['1','2','3','4','a',(1,2,3),1,[1,2],{"a":1}]
2 print(tuple(list))
3 ('1', '2', '3', '4', 'a', (1, 2, 3), 1, [1, 2], {'a': 1})

3、列表转换成字典

1 list1 = ['1','2','3','4','a',(1,2,3),5,6]
2 list2 = ['a','b','c','d','e','f',[1,2],{"g":"h"}]
3 dict1 = dict(zip(list1,list2))
4 print(dict1)
5 {'1': 'a', '2': 'b', '3': 'c', '4': 'd', 'a': 'e', (1, 2, 3): 'f', 5: [1, 2], 6: {'g': 'h'}}

4、列表转换成集合

无序的,去重的

1 list = ['1','2','3','3']
2 print(set(list))
3 {'2', '1', '3'}

三、元祖的转换

1、转换成字符串

1 tuple = ('1','2','3','4','5','2','a')
2 print(''.join(tuple))
3 123452a

2、转换成列表

1 tuple = (1,2,3,4,5,'2','a')
2 print(list(tuple))
3 [1, 2, 3, 4, 5, '2', 'a']

3、转换成字典

1 tuple1 = ('1', '2', '3', '4',111,(11,22,33),"")
2 tuple2 = ('a', 'b', 'c', 'd','e','f','h','g','i','j','k')
3 dict1 = dict(zip(tuple1, tuple2))
4 print(dict1)
5 {'1': 'a', '2': 'b', '3': 'c', '4': 'd', 111: 'e', (11, 22, 33): 'f', '': 'h'}

4、转换成集合 

1 tuple1 = ('1','2','3','4',4,'4')
2 print(set(tuple1))
3 {'1', 4, '2', '4', '3'}

四、字典的转化

1、转换成字符串

对于生成字符串,需要先生成list和tuple,然后再由list和tuple生成str

2、转换成列表/元祖/集合

字典可以使用 dict.keys() 和dict.values()返回迭代器,通过list和tuple直接生成列表和元祖

 1 dict1 = {1:'a',2:'b',3:'c'}
 2 print(list(dict1.keys()))
 3 print(list(dict1.values()))
 4 print(tuple(dict1.keys()))
 5 print(tuple(dict1.values()))
 6 print(set(dict1.keys()))
 7 print(set(dict1.values()))
 8 print(tuple(dict1.items()))  #生成元祖为单位的元祖
 9 print(list(dict1.items()))  #生成元祖为单位的列表
10 print(set(dict1.items()))   #生成元祖为单位的集合
11 [1, 2, 3]
12 ['a', 'b', 'c']
13 (1, 2, 3)
14 ('a', 'b', 'c')
15 {1, 2, 3}
16 {'b', 'c', 'a'}
17 ((1, 'a'), (2, 'b'), (3, 'c'))
18 [(1, 'a'), (2, 'b'), (3, 'c')]
19 {(2, 'b'), (3, 'c'), (1, 'a')}

五、集合的转换

1、转换成字符串

列表无序,每次转换成的字符串值都不一定一样,也可以先转为列表或者元祖,再转换成字符串

1 set1 = {"1","2","aaa"}
2 print("".join(set1))
3 print(type("".join(set1)))
4 aaa12
5 <class 'str'>

2、转换成列表/元祖

1 set1 = {1,2,3,4,5,6}
2 print(list(set1))
3 print(tuple(set1))
4 [1, 2, 3, 4, 5, 6]
5 (1, 2, 3, 4, 5, 6)

3、转换成字典

无意义的映射,不推荐

dict(zip(set1,set2))

常用转换方法总结:

转换成字符串:"".join()

转换成列表:list()

转换成元祖:tuple()

转换成字典:dict(zip(a,b))

转换成集合:set()

欢迎补充,谢谢~

原文地址:https://www.cnblogs.com/ClarenceSun/p/12670402.html