zoukankan      html  css  js  c++  java
  • 各数据类型之间的转化

    元祖跟列表之间的转化:

    tu = (1, 2, 3)
    l = list(tu)
    print(l, type(l))
    
    li = [4, 5, 6]
    print(tuple(li),type(tuple(li)))
    
    输出:
    [1, 2, 3] <class 'list'>
    (4, 5, 6) <class 'tuple'>


    字符串转化成列表:

    split:

    s = "ab_c"
    lst = s.split("_")
    print(lst)
    
    输出:
    ['ab', 'c']

    或者:

    s = "ab_c"
    print(list(s))
    
    输出:
    ['a', 'b', '_', 'c']

    列表转化成字符串:

    join

    lst = ["a", "b", "c", "d"]
    s = "*".join(lst)
    print(s)
    
    输出:
    a*b*c*d

    或者:

    lst = ["a", "b", "c", "d"]
    s = str(lst)
    print(s,type(s))

    输出:
    ['a', 'b', 'c', 'd'] <class 'str'>

    字符串转化成元祖:

    s = "ab_c"
    t = tuple(s)
    print(t, type(t))
    
    输出:
    ('a', 'b', '_', 'c') <class 'tuple'>

    1, int ------> str:    str(int)

    2, str------->int:     int(str) #字符串必须全部由数字组成

    3,bool ------->str:    str(bool)

    4,str -------->bool  除了空字符串,剩下的都是True

    5,int -------->bool 除了0,剩下的全是True

    6,bool ------->int  True----->1,False ----->0

    7,str --------> list     #split

    8,list -------->str     #join

    9,tuple-------->list:    list(tuple)

    10,list --------->tuple:  tuple(list)

    11,str ------>list:    list(str)

    12,str ------>tuple  tuple(str)

  • 相关阅读:
    为什么整个互联网行业都缺前端工程师?
    css3做的圆特效
    又一个前端的小渣渣诞生了
    返回顶部代码!
    网页动画的十二原则
    JQuery缓冲加载图片插件lazyload.js的使用方法
    关于写手机页面demo的准备工作
    HTML5加载动画
    HTML5加载动画
    正则表达式语法
  • 原文地址:https://www.cnblogs.com/biluo/p/7787118.html
Copyright © 2011-2022 走看看