在符合条件下,python中的字符型,元组,列表,集合,字典等数据类型可以相互转化。
1,字符型--->元组 (str---->tuple, 字符串中每个字符被拆开保存到元组中)
>>> str1 = "This is a new book."
>>> tuple(str1)
('T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'n', 'e', 'w', ' ', 'b', 'o', 'o', 'k', '.')
>>> str1
'This is a new book.'
>>>
2,字符型-->集合(str--->set,字符串中每个字符被拆开,无重复的保存到元组中 )
>>> set(str1)
{'T', 's', ' ', 'k', 'e', 'a', '.', 'n', 'i', 'h', 'b', 'w', 'o'}
>>>
3,字符型--->列表(str--->list, 用list()强转时和元组效果一样,也可以用函数str.split([sep])转化)
>>> list(str1)
['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'n', 'e', 'w', ' ', 'b', 'o', 'o', 'k', '.']
>>>
>>> str1 = 'This is a new book.'
>>> str1.split()
['This', 'is', 'a', 'new', 'book.']
>>>
4,列表---->字符( list--->str,列表中所有元素,包括中括号,单引号,都转化为字符串中的字符
["sep"].join(iterable)函数可以实现从列表、字典、元组到字符串)
>>> list_a = ["I" , "like", "my", "watch", ]
>>> str_b = str(list_a)
>>> str_b
"['I', 'like', 'my', 'watch']"
>>> str_b[0]
'['
>>> str_b[0:3]
"['I"
>>>
>>> "".join(list_a)
'Ilikemywatch'
>>> ",".join(list_a)
'I,like,my,watch'
>>> " ".join(list_a)
'I like my watch'
>>>
5. 列表--->元组(tuple()强转)
>>> list_a = ["I" , "like", "my", "watch", ]
>>> tuple_a = tuple(list_a)
>>> tuple_a
('I', 'like', 'my', 'watch')
>>>
6. 列表--->集合( set()强转,列表中不能出现列表,字典等类型元素)
>>> list_a = ["I" , "like", "my", "watch", ]
>>> set(list_a)
{'I', 'like', 'watch', 'my'}
>>>
7. 元组--->列表
>>> tuple_a = ("I" , "like", "my", "watch", ['cctv', "cntv"] )
>>> list(tuple_a)
['I', 'like', 'my', 'watch', ['cctv', 'cntv']]
>>>
8. 元组---> 字符串
>>> str(tuple_a)
"('I', 'like', 'my', 'watch', ['cctv', 'cntv'])"
>>>
9. 元组--->集合( set()强转,列表中不能出现列表,字典等类型元素)
>>> tuple_a = ("I" , "like", "my", "watch", 'cctv', )
>>> set(tuple_a)
{'watch', 'like', 'cctv', 'I', 'my'}
>>>
10. 集合--->字符串(集合中所有元素,包括花括号,单引号,都转化为字符串中的字符)
>>> set_a = {'watch', 'like', 'cctv', 'I', 'my'}
>>> str(set_a)
"{'watch', 'like', 'cctv', 'I', 'my'}"
>>>
11.集合--->元组
>>> set_a = {'watch', 'like', "cctv", 'I', 'my'}
>>> tuple(set_a)
('watch', 'like', 'cctv', 'I', 'my')
>>>
12.集合--->列表
>>> set_a = {'watch', 'like', "cctv", 'I', 'my'}
>>> list(set_a)
['watch', 'like', 'cctv', 'I', 'my']
>>>
==================分隔符===================
由于字典数据结构是key:value形式,因此在类型转化时有所不同。
1, 字典---->列表时,返回列表key
>>> dict_a = {"Monkey": "banana", "Panda":"bamboo", "horse":"grass", "sheep": "grass"}
>>> list(dict_a)
['Monkey', 'Panda', 'horse', 'sheep']
>>>
2, 字典---->字符串时,包括花括号,单引号,都转化为字符串中的字符
>>> str(dict_a)
"{'Monkey': 'banana', 'Panda': 'bamboo', 'horse': 'grass', 'sheep': 'grass'}"
>>>
3,字典---->元组(返回key作为元组元素)
>>> tuple(dict_a)
('Monkey', 'Panda', 'horse', 'sheep')
>>>
4,字典--->集合(返回key作为集合元素)
>>> set(dict_a)
{'Monkey', 'horse', 'sheep', 'Panda'}
>>>
5,元组和列表元素在符合一定条件时可以转为字典
>>> tp1 = (('Monkey','banana'), ('Panda','bamboo'), ('horse','grass'), ('sheep','grass'))
>>> dict(tp1)
{'Monkey': 'banana', 'Panda': 'bamboo', 'horse': 'grass', 'sheep': 'grass'}
>>>
>>> list_a = [['Monkey','banana'], ['Panda','bamboo'], ['horse','grass'], ['sheep','grass']]
>>> dict(list_a)
{'Monkey': 'banana', 'Panda': 'bamboo', 'horse': 'grass', 'sheep': 'grass'}
>>>