美文网首页
一 -18 python (基础)列表的镶嵌

一 -18 python (基础)列表的镶嵌

作者: hsiaojun | 来源:发表于2018-04-19 00:49 被阅读0次

列表嵌套

  • 类似while循环的嵌套,列表也是支持嵌套的

  • 一个列表中的元素又是一个列表,那么这就是列表的嵌套
    叫做二维列表

      遍历列表是有顺序的
      列表存储相同类型的数据
    
    import random
    
    # 定义一个列表用来保存3个办公室
    offices = [[],[],[]]
    
    # 定义一个列表用来存储8位老师的名字
    names = ['A','B','C','D','E','F','G','H']
    
    # 完成随机分配
    i = 0
    for name in names:
        index = random.randint(0,2)    
        offices[index].append(name)
    
    # 获取办公室信息
    i = 1
    for tempNames in offices:
        print('办公室%d的人数为:%d'%(i,len(tempNames)))
        i+=1
        for name in tempNames:
            print("%s"%name,end='')
        print("\n")
        print("-"*20)

相关文章

网友评论

      本文标题:一 -18 python (基础)列表的镶嵌

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