数据类型转换总结
1.字典
dic = {"k1":"v1","k2":"v2","k3":[11,22,33]}
print(str(dic)) #转字符串 感觉无区别 返回: {'k1': 'v1', 'k2': 'v2', 'k3': [11, 22, 33]}.
print(tuple(dic)) #转元祖. 返回: ('k1', 'k2', 'k3')
print(list(dic)) #转列表. 返回: ['k1', 'k2', 'k3']
2.元祖
tup = (1,2,3,4,5)
print(str(tup)) #转字符串. 感觉无区别 返回: (1, 2, 3, 4, 5)
print(list(tup)) #转列表. 返回: [1, 2, 3, 4, 5]
#元祖不能转换为字典
3.列表
nums = [1,3,5,7,8,13,20]
print("".join("%s" %id for id in nums))#转字符串. 返回: 135781320
print(str(nums)) #转字符串 返回:[1, 3, 5, 7, 8, 13, 20] 感觉无区别
nums = ["huh","snn"] #列表内元素只能为字符串,不能为数字
list1 = " ".join(nums) #通过join转字符串
print(list1) # 返回: huh snn
print(tuple(nums)) #转元祖. 返回:(1, 3, 5, 7, 8, 13, 20)#列表不能转换为字典
4.字符串
s = "alex" #目前只学会针对单个字符串转换元祖的操作
b=tuple(s.split()) #定义一个参数.
print(b ) #转为字符串. 返回: ('alex',)
print(s.split()) #转列表. 返回: ['alex']
print (type(eval("{'name':'srx','age':'41'}")))#转字典 返回:<class 'dict'>