当前位置: 首页 > 后端技术 > Python

Python3常用数据类型转换

时间:2023-03-26 16:45:03 Python

1.数据类型转换,只需要将数据类型作为函数名就可以得到整型二进制的4种表示形式:以'0b'开头。例如:“0b11011”表示278base10:以“0o”开头。例如:'0o33'表示十进制2710:正常显示十六进制:以'0x'开头。例如:'0x1b'表示十进制274个碱基的转换:转换是通过python中的内置函数(bin,oct,int,hex)实现的2.列表、元组、集合、字典的相互转换1.ListsTupletootherListtoset(removeduplicates)list1=[6,7,7,8,8,9]print(set(list1))Python3result:{6,7,8,9}两个listtodictionarylist1=['key1','key2','key3']list2=['1','2','3']print(dict(zip(list1,list2)))Python3结果:{'key1':'1','key2':'2','key3':'3'}嵌套列表到字典list3=[['key1','value1'],['key2','value2'],['key3','value3']]print(dict(list3))Python3结果:{'key1':'value1','key2':'value2','key3':'value3'}列表,元组到字符串list2=['a','a','b']print(''.join(list2))Python3结果:aabtup1=('a','a','b')print(''.join(tup1))Python3result:aab2,dictionarytootherdictionariesconvertedtostringdic1={'a':1,'b':2}print(str(dic1))Python3结果:{'a':1,'b':2}字典键值转换dic2={'a':1,'b':2,'c':3}print({value:keyforkey,valueindic2.items()})Python3结果:{1:'a',2:'b',3:'c'}3.StringtootherStringtolists='aabbcc'print(list(s))Python3结果:['a','a','b','b','c','c']Stringtotupleprint(tuple(s))Python3result:('a','a','b','b','c','c')要设置的字符串打印(集合(s))Python3结果:{'a','b','c'}字符串到字典s="{'name':'Tom','age':18}"dic2=eval(s)print(dic2)Python3结果:{'name':'Tom','age':18}a='{"name":"Rose","age":19}'print(eval(a))Python3结果:{'name':'Rose','age':19}以上就是本次分享的全部内容。现在想学习编程的朋友欢迎关注Python技术大本营获取更多技能教程