• 列表


    一、基础格式

    1. 列表格式:中括号括起来,","分割每个元素

    2. 列表中可以嵌套任何类型:数字,字符串,列表,布尔值

    3、“集合”,内部放置任何东西

    二、函数及用法
    ① 索引取值/切片

     

    li = [12,'dji','ddejio',['djio',123],True]
    print(li[3])
    print(li[2:5])

    输出结果:

    ['djio', 123]
    ['ddejio', ['djio', 123], True]

    ② for #循环

     

    li = [12,'dji','ddejio',['djio',123],True]
    for item in li :
        print(item)

    输出结果:

    12
    dji
    ddejio
    ['djio', 123]
    True

    ③ del #删除

     

    li = [12,'dji',False,'ddejio',['djio',123],True,["cone","djj",('jio')]]
    print(li)
    del li[6]
    print(li)
    del li[1:3] 
    print(li)

    输出结果:

    [12, 'dji', False, 'ddejio', ['djio', 123], True, ['cone', 'djj', 'jio']]
    [12, 'dji', False, 'ddejio', ['djio', 123], True]
    [12, 'ddejio', ['djio', 123], True]


    ④ [] #修改/替换

    li = [12,'dji','ddejio',['djio',123],True]
    print(li)
    li[3] = 567 #直接修改
    print(li)
    li[3:5] = [123,567,789] #切片式修改
    print(li)

    输出结果:

    [12, 'dji', 'ddejio', ['djio', 123], True]
    [12, 'dji', 'ddejio', 567, True]
    [12, 'dji', 'ddejio', 123, 567, 789]

    ⑤ in #判断列表中是否存在该元素

    li = [12,'dji','ddejio',['djio',123],True]
    print(li)
    v1 = 'dji' in li
    print(v1)
    v2 = 56 in li
    print(v2)

    输出结果:

    [12, 'dji', 'ddejio', ['djio', 123], True]
    True
    False

    ⑥ 操作

    li = [12,'dji','ddejio',['djio',["19",56],123],True]
    v = li[3][1][0]
    print(v)

    输出结果:19

    二、列表与字符串互相转换

    1 .join #直接使用字符串join方法:列表中的元素只有字符串

    li = ['dji','ddejio']
    print(li)
    v = "QQ".join(li)
    print(v)

    输出结果:

    ['dji', 'ddejio']
    djiQQddejio

    2 .for循环 #一个一个处理: 既有数字又有字符串

    li = ['dji','ddejio',123]
    s = ''
    for i in li :
        s = s + str(i)
    print(s)

    输出结果:djiddejio123

  • 相关阅读:
    算法导论2.37答案
    算法导论2.37的算法
    heavy dark读《《暗时间》》
    深入SetOP2函数
    c++标准库都有哪些文件
    c++ sort函数的用法
    深入char转换为int/unsigned int的内部机制分析
    顺序容器之vector
    java的动态代理机制详解
    java.lang.IllegalStateException: Web app root system property already set to different value
  • 原文地址:https://www.cnblogs.com/lishuangtu/p/8884337.html
Copyright © 2020-2023  润新知