zoukankan      html  css  js  c++  java
  • python 数据类型 转换

     转换为整数
    # a = int(3.14)#3
    # print(a)
    # # 参数1:需要转换的数据
    # # base:数据的进制类型,默认为十进制
    # a = int('123', base=8)#83
    # print(a)
    # a = int('abc', base=16)#2748
    # print(a)
    # 浮点
    a = float(250)#250.0
    
    print(a)
    
    # 字符串
    a = str(123)
    print(a,type(a))#123 <class 'str'>
    
    # 列表
    a = list('hello')
    b=''.join(a)
    print(b,type(b))#hello <class 'str'>
    print(a,type(a))#['h', 'e', 'l', 'l', 'o'] <class 'list'>
    c=(1,2,3)
    print(type(c))#<class 'tuple'>
    a = list((1, 2, 3))#元组转列表
    print(a,type(a))
    d={1,2,3}
    print(type(d))#<class 'set'>
    a = list({1, 2, 3})
    print(a,type(a))#[1, 2, 3] <class 'list'>

    # 可以转换不会报错,但是只保留了键['name', 'age']
    # a = list({'name': 'ergou', 'age': 18})
    # print(a)
    #
    # # 元组
    # a = tuple([1, 2, 3])
    # print(a)#(1, 2, 3)
    # 集合
    # a = set([1, 2, 3])#列表转集合{1, 2, 3};集合没有下标
    # print(a)
    # 字典
    # lt = [('name', 'dahua'), ('age', 18)]
    # print(type(lt))
    # a = dict(lt)#列表转字典
    # print(a, type(a))#{'name': 'dahua', 'age': 18} <class 'dict'>
    # 
  • 相关阅读:
    七夕祭
    Running Median
    电影Cinema
    Best Cow Fences
    Sumdiv
    Tallest Cow
    激光炸弹
    Strange Towers of Hanoi
    Gerald and Giant Chess
    CF24D Broken robot
  • 原文地址:https://www.cnblogs.com/liangliangzz/p/10134191.html
Copyright © 2011-2022 走看看