美文网首页
list基本操作

list基本操作

作者: 她和她的喵真好看 | 来源:发表于2019-03-16 12:56 被阅读0次

    1、轮询

    1.1 根据下标轮询

    list = ["1","2","3","4"]
    for i in range(len(list)):
     print("list[i]", list[i])
    

    1.2 根据值轮询

    list = ["1","2","3","4"]
    for data in(list):
      print("data",data)
    

    2、切片

    2.1 代码

    list = [1,2,3,4,5,6,7]
    print("list[1:3] = ",list[1:3])
    print("list[-1:] = ",list[-1:])
    print("list[:-1] = ",list[:-1])
    print("list[:3] = ",list[:3])
    print("list[3:] = ",list[3:])
    
    打印信息:
    list[1:3] =  [2, 3]
    list[-1:] =  [7]
    list[:-1] =  [1, 2, 3, 4, 5, 6]
    list[:3] =  [1, 2, 3]
    list[3:] =  [4, 5, 6, 7]
    

    3、tuple 和list 相互转化

    tupleData = tuple(list)
    print("tupleData = ",tupleData)
    listData = list(tupleData)
    print("listData = ", listData)
    
    打印信息:
    tupleData =  (1, 2, 3, 4, 5, 6, 7)
    listData =  [1, 2, 3, 4, 5, 6, 7]
    

    4、用某个固定值初始化列表

    initial_value = 0
    list_length = 5
    sample_list = [ initial_value for i in range(10)]
    sample_list = [initial_value]*list_length
    # sample_list ==[0,0,0,0,0]
    

    相关文章

      网友评论

          本文标题:list基本操作

          本文链接:https://www.haomeiwen.com/subject/oegjmqtx.html