zoukankan      html  css  js  c++  java
  • 01 列表内置方法

    ''''''
    '''
    列表:
        在[]内,可以存放多个任意类型的值,并以逗号隔开。
        一般用于存放学生的爱好,课堂的周期等等
    '''
    #定义一个学生列表,可存放多个学生
    #本质上是list(['钱垚','张天爱','邢菲','林一'])
    students = ['钱垚','张天爱','邢菲','林一']
    print(students[1])
    
    student_info = ['林一',19,'smale',['滑板','跳舞']]
    #取林一同学所有爱好
    print(student_info[3])  #['滑板', '跳舞']
    #取林一同学的第二个爱好
    print(student_info[3][1])   #跳舞
    
    
    '''
    优先掌握的操作:
        1、按索引取值(正向存取+反向存取):即可存也可取
        2、切片(顾头不顾尾,步长)
        3、长度len
        4、成员运算in和not in
        5、追加 append(只能追加末尾)
        6、删除 del
        7、循环
    '''
    #按索引取值(正向存取+反向存取):即可存也可取
    print(student_info[-2]) #smale
    
    #切片(顾头不顾尾,步长)
    print(student_info[0:4:2])  #['林一', 'smale']
    
    #长度len
    print(len(student_info))    #4
    
    #成员运算in和not in
    print('林一' in student_info) #True
    print('林一' not in student_info) #False
    
    #追加
    #在student_info列表末尾追加一个值
    student_info.append('合肥学院')
    print(student_info) #['林一', 19, 'smale', ['滑板', '跳舞'], '合肥学院']
    
    #删除
    #删除列表索引为2的值
    del student_info[2]
    print(student_info) #['林一', 19, ['滑板', '跳舞'], '合肥学院']
    
    #需要掌握的
    student_info = ['林一',19,'smale',['滑板','跳舞'],19]
    #1、获取列表中某个值的索引
    print(student_info.index(19))   #1
    
    #2、count 获取列表中某个值的数量
    print(student_info.count(19))    #2
    
    #3、pop取值,括号中是空的默认取列表中最后一个值,类似删除
    #若pop()括号中写了索引,则取索引对应的值
    student_info.pop()
    print(student_info) #['林一', 19, 'smale', ['滑板', '跳舞']]
    student_info.pop(2)
    print(student_info) #['林一', 19, ['滑板', '跳舞']]
    #去除列表中索引为2的值,并赋值给sex
    student_info = ['林一',19,'smale',['滑板','跳舞'],19]
    sex = student_info.pop(2)
    print(sex)  #smale
    print(student_info) #['林一', 19, ['滑板', '跳舞'], 19]
    
    #4、移除,把列表中的某个值的第一个值移除
    student_info.remove(19)
    print(student_info) #['林一', ['滑板', '跳舞'], 19]
    
    name = student_info.remove('林一')
    print(name) #None
    print(student_info) #[['滑板', '跳舞'], 19]
    
    #5、插入值
    student_info = ['林一',19,'smale',['滑板','跳舞'],19]
    #在student_info中,索引为3的位置插入合肥学院
    student_info.insert(3,'合肥学院')
    print(student_info) #['林一', 19, 'smale', '合肥学院', ['滑板', '跳舞'], 19]
    
    #6、extend 合并列表
    student_info1 = ['林一',19,'smale',['滑板','跳舞'],19]
    student_info2 = ['邢菲',20,'smale',['滑板','跳舞']]
    #把student_info2所有的值插入student_info1内
    student_info1.extend(student_info2)
    print(student_info1)    #['林一', 19, 'smale', ['滑板', '跳舞'], 19, '邢菲', 20, 'smale', ['滑板', '跳舞']]
    
    #循环
    for student in student_info:
        print(student)
  • 相关阅读:
    C#下编程完成IIS网络App的权限设置
    IIS6与IIS7在编程实现HTTPS绑定时的细微差别
    Android 对话框(Dialog)大全
    Android 开发中使用Intent传递数据的方法
    设计模式--模版设计模式
    android 布局页面文件出错故障排除Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V
    viewPager的切换动画
    设计模式--状态模式
    git学习
    二〇一五年五月二十二日--bug--启动页面出现模糊的问题
  • 原文地址:https://www.cnblogs.com/urassya/p/11083543.html
Copyright © 2011-2022 走看看