python类型转换字符_python数据类型转换
1.list转str
列表转换为字符串
num = [1,2,3,4]
.join(num)
输出:‘1234’
注意:当列列表内的元素为数字的时候,需要先转成字符型,在进行转换。
2.str转换成list
num = ‘1234’
list(num)
输出:[1,2,3,4]
3.tuple转换成list
4.list转换成tuple
5.list转换为dict
(1)先通过zip()函数李亮亮组合成键值对
(2)嵌套list转换为dict
6.dict转换成list
把字典的key和value分开成两个列表展示
7.list转换成set
使用set()函数,顺便会把list内的重复元素去除
8.tuple转换为str
9.字符串转换成列表、元组、集合、字典
# 字符串转列表
s = aabbcc
list(s)
# [a, a, b, b, c, c]
# 字符串转元组
tuple(s)
# (a, a, b, b, c, c)
# 字符串转集合
set(s)
# {a, b, c}
# 字符串转字典
dic2 = eval("{name:haha, age:18}")
