• 比较全面的python类型转换


     前言提示:

    int(整形):a=1

    str(字符串):a="asdsdf地方123"

    float(浮点):a="3.14150"

    bytes(字节包):a=b'xe2x82xac20'

    complex(复数):a=(34567+0j)

    list(列表):a=[1, 2, 3, '4', '五', 'liu']

    set(集合):a={1,2,2,3,"4","五","liu"}

    tuple(元组):a=(1,2,2,3,"4","五","liu")

    dict(字典):a={'id': '4', 'start': '0','td': '55'}

      

    1.int类型转换(支持str,float,bytes)

    ①str转int(如果有0到9还有+-以外的数字会报错)

    a="-1234567"
    print(a)
    print(type(a))
    int("-1234567")
    print(int("-1234567"))
    print(type(int("-1234567")))

    或者

    a="-1234567"
    print(a)
    print(type(a))
    int(a)
    print(int(a))
    print(type(int(a)))

    ②float转int

    a=1.2
    print(a)
    print(type(a))
    int(1.2)
    print(int(1.2))
    print(type(int(1.2)))

    或者

    a=1.2
    print(a)
    print(type(a))
    int(a)
    print(int(a))
    print(type(int(a)))

     

    ③bytes转int(如果有0到9还有+-.以外的数字会报错)

    a=b'13579'
    print(a)
    print(type(a))
    int(b'13579')
    print(int(b'13579'))
    print(type(int(b'13579')))

    或者

    a=b'13579'
    print(a)
    print(type(a))
    int(a)
    print(int(a))
    print(type(int(a)))

    2.float类型转换(支持str,int,bytes)

    ①str转float(如果有0到9还有+-以外的数字会报错)

    a="31415"
    print(a)
    print(type(a))
    float(a)
    print(float(a))
    print(type(float(a)))

    ②int转float

    a=2468
    print(a)
    print(type(a))
    float(a)
    print(float(a))
    print(type(float(a)))

    ③bytes转float

    a=b"34567"
    print(a)
    print(type(a))
    float(a)
    print(float(a))
    print(type(float(a)))

     

    3.complex类型转换(支持int,str,float)

    ①int转complex

    a=34567
    print(a)
    print(type(a))
    complex(a)
    print(complex(a))
    print(type(complex(a)))

    ②str转complex(若能转换成)

    a="-3.1415"
    print(a)
    print(type(a))
    complex(a)
    print(complex(a))
    print(type(complex(a)))

    a="3.0"
    print(a)
    print(type(a))
    complex(a)
    print(complex(a))
    print(type(complex(a)))

    小数点被去除了

    ③float转complex

    a=3.1415
    print(a)
    print(type(a))
    complex(a)
    print(complex(a))
    print(type(complex(a)))

    4.str类型转换(所有对象都可以转)

    ①int转str(直接转换)

    a=6
    print(a)
    print(type(a))
    str((a))
    print(str(a))
    print(type(str((a))))

    ②float转str(去除末尾0的部分)

    a=6.50
    print(a)
    print(type(a))
    str((a))
    print(str(a))
    print(type(str((a))))

    ③complex转str(把值转化为标准的 complex 表达式,然后再转换为字符串)

    a=6.50+1j
    print(a)
    print(type(a))
    str((a))
    print(str(a))
    print(type(str((a)))

    ④bytes转str

    a=b"231qweq"
    print(a)
    print(type(a))
    str((a))
    print(str(a))
    print(type(str((a))))

    ⑤list转str(把值格式化为标准的 list 表达式,然后再转换为字符串)

    a=[1,3,5,1,"abc","aabbcc"]
    print(a)
    print(type(a))
    str((a))
    print(str(a))
    print(type(str((a))))

    ⑥tuple转str(把值格式化为标准的 tuple 表达式,然后再转换为字符串)

    a=(1,"ab",2,"cd",3)
    print(a)
    print(type(a))
    str((a))
    print(str(a))
    print(type(str((a))))

    ⑦dict转str(可以正常转换,或者通过''.join(值)单独提出key值)

    a={ 'id': '4','start': '0','iv': '55'}
    print(a)
    print(type(a))
    str((a))
    print(str(a))
    print(type(str((a))))
    print("".join(a))
    print(type("".join(a)))

    ⑧set转str

    a={1,1,3,3,2,2}
    print(a)
    print(type(a))
    str((a))
    print(str(a))
    print(type(str((a))))
    print("".join({"a","b","c"}))
    print(type("".join({"a","b","c"})))

    5.bytes类型转换(支持str)

    ①str转bytes

    a="地球"
    print(a)
    print(type(a))
    bytes(a.encode())
    print(bytes(a.encode()))
    print(type(bytes(a.encode())))

    6.list类型转换(支持str,dict,set,tuple等序列)

    ①str转list

    a="地球火星蜀西"
    print(a)
    print(type(a))
    list(a)
    print(list(a))
    print(type(list(a)))

     

    ②dict转list

    a={'id': '4','start': '0','iv': '55'}
    print(a)
    print(type(a))
    list(a)
    print(list(a))
    print(type(list(a)))

    ③set转list

    a={1,2,3,4,2,3,5}
    print(a)
    print(type(a))
    list(a)
    print(list(a))
    print(type(list(a)))

    ④tuple转list

    a=(1,2,3)
    print(a)
    print(type(a))
    list(a)
    print(list(a))
    print(type(list(a)))

    7.tuple类型转换(支持str,bytes,list,dict,set)

    ①str转tuple

    a="荷兰豆"
    print(a)
    print(type(a))
    tuple(a)
    print(tuple(a))
    print(type(tuple(a)))

    ②bytes转tuple(bytes 转换元组,会取每个字节的 ASCII 十进制值并组合成列表)

    a=b"hello word"
    print(a)
    print(type(a))
    tuple(a)
    print(tuple(a))
    print(type(tuple(a)))

    ③list转tupe

    a=[1,2,"er",4]
    print(a)
    print(type(a))
    tuple(a)
    print(tuple(a))
    print(type(tuple(a)))

    ④dict转tuple

    a={'id': '4', 'start': '0','iv': '55'}
    print(a)
    print(type(a))
    tuple(a)
    print(tuple(a))
    print(type(tuple(a)))

     ⑤set转tuple

    a={1,2,2,3,3,4,5,"adv"}
    print(a)
    print(type(a))
    tuple(a)
    print(tuple(a))
    print(type(tuple(a)))

    8.dict类型转换(支持str,list,set,tuple)

    ps:dict必须是键值对形式的key对应value

    ①str转dict(两种方式一种关键字eval和json)

    第一种:eval

    a = '{"id": "4", "start": "0","iv": "55","limit": "25"}'
    print(a)
    print(type(a))
    b=eval(a)
    print(b)
    print(type(b))

    第二种json:

    a = '{"id": "4", "start": "0","iv": "55","limit": "25"}'
    print(a)
    print(type(a))
    json.loads(a)
    print(json.loads(a))
    print(type(json.loads(a)))

    ②list转dict(使用关键字zip映射)

    a=["a","b","c"]
    b=[1,2,3]
    print(a)
    print(type(a))
    dict(zip(a,b))
    print(dict(zip(a,b)))
    print(type(dict(zip(a,b))))

    ③set转dict(嵌套转换)

    a={1,2,3}
    b={"a","b","c"}
    print(a)
    print(type(a))
    dict(zip(a,b))
    print(dict(zip(a,b)))
    print(type(dict(zip(a,b))))

    ④tuple转dict

    a=(("a",1),("b",2),("c",3))
    print(a)
    print(type(a))
    dict(a)
    print(dict(a))
    print(type(dict(a)))

    9.set类型转换(支持str,list,bytes,tuple,dict)

    ①str转set(先切割成元祖在转集合,去重)

    a="hello"
    print(a)
    print(type(a))
    set((a))
    print(set(a))
    print(type(set((a))))

    ②list转set(先去重在转换)

    a=[3,1,2,"ren",3]
    print(a)
    print(type(a))
    set((a))
    print(set(a))
    print(type(set((a))))

    ③bytes转set(取每个字节的 ASCII 十进制值并组合成元组,再去重)

    a=b"123rtyrty"
    print(a)
    print(type(a))
    set((a))
    print(set(a))
    print(type(set((a))))

    ④tuple转set(去重,在转换)

    a=(1,2,1,"nb",4)
    print(a)
    print(type(a))
    set((a))
    print(set(a))
    print(type(set((a))))

    ⑤dict转set(只能取key值做成集合,而且是无序排列的)

    a={'id': '4', 'start': '0', 'iv': '55'}
    print(a)
    print(type(a))
    set((a))
    print(set(a))
    print(type(set((a))))

  • 相关阅读:
    iis 部署网站常见问题
    提高你开发效率的十五个Visual Studio 2010使用技巧
    SQL SERVER 错误代码 0x534
    SQL Server2005修改计算机名后不能发布订阅
    Fedora桌面系统Idea中的部分中文无法显示解决
    最近去滴滴面试的一道题:斐波拉契数列非递归实现
    二叉查找树之红黑树
    二叉查找树之AVL树
    二叉查找树
    再看SpringMVC通过一个DispatcherServlet处理Servlet
  • 原文地址:https://www.cnblogs.com/wtx0/p/9759567.html
Copyright © 2020-2023  润新知