zoukankan      html  css  js  c++  java
  • Python基础语法 第3节课 (列表)

    [ ]列表

    一、列表可以存放的内容

    1.列表可以存放哪些类型?

     列表是一个大仓库,百宝箱,所学过的所有类型,都可以放在列表里面。

    my_list = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
    print(my_list)

    结果:[1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python']]

    2.列表的长度len()

    my_list = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
    print(len(my_list)) #7

    3.获取列表中的元素---索引(类似字符串)

    列表索引得到的结果还是列表(该元素是什么数据类型,索引得到的结果就是什么类型,不会发生变化)

    my_list = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
    print(my_list[-1][1]) # python

    4.列表的切片,还是个列表

    二、创建列表

    空列表

    number = []
    print(number) #[]

    普通列表:

    number = [1,2,3]
    print(number) 

    混合列表:

    list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
    print(list_1)  # [1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python']]

    列表的常用操作:增删改

    三、向列表中添加元素

    1.append() :默认在列表末尾追加新的对象。即将参数作为一个元素增加到列表尾部。(append的参数只有一个,append一次只能添加一个元素)

    list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
    list_1.append(9)
    print(list_1)  # [1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python'], 9]

     ❤append()是对列表有作用,但是其返回的结果是None,这个是python的规则。(remove() 同理.)

    my_list = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
    new_elem = my_list.append(888)
    print(my_list) 
    print(new_elem)

    结果:
    [1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python'], 888]
    None

    2.extend():在列表的尾部一次性追加另一个序列中的多个值,即将参数作为一个列表去扩展原列表,列表的合并。

    list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
    list_1.extend([2,4,5,6])
    print(list_1)  #[1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python'], 2, 4, 5, 6]

    3.insert():有2个参数,第一个参数是待插入的位置,第二个参数是在该待插入位置,插入的值。

    list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
    list_1.insert(3,'hello world')
    print(list_1)  #[1, 2, 3, 'hello world', (2, 'polo'), 6, 'hello', ['lemon', 'python']]

    四、删除列表中的元素

    1.删除指定的内容(不知道索引的位置,只知道列表中有这个具体的内容,一定是 有 才可以删除,列表中不存在,是无法操作的)

    list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
    list_1.remove(['lemon','python'])
    print(list_1)  # [1, 2, 3, (2, 'polo'), 6, 'hello']

    remove()只能移除一个元素,列表中有多个相同元素,默认是移除第一个

    list_1 = [1,'hello',2,'hello',3,(2,'polo'),6,'hello',['lemon','python']]
    list_1.remove('hello')
    print(list_1)  #[1, 2, 'hello', 3, (2, 'polo'), 6, 'hello', ['lemon', 'python']]

    2.删除指定的索引位置pop(),返回的结果是删除的元素值

    list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
    #pop()不带索引值,默认移除列表中最后一个元素
    list_1.pop()
    print(list_1) # [1, 2, 3, (2, 'polo'), 6, 'hello']
    list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
    #pop()中带索引值,删掉对应索引值的内容
    list_1.pop(3)
    print(list_1)# [1, 2, 3, 6, 'hello', ['lemon', 'python']]

    ❤查看pop()的结果(返回值)

    list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
    elem = list_1.pop()
    print(elem)
    print(list_1)

    结果:

    ['lemon', 'python']

    [1, 2, 3, (2, 'polo'), 6, 'hello']

    五、修改列表中的元素

    list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
    list_1[2] = 'hahhahahha'
    print(list_1)  #[1, 2, 'hahhahahha', (2, 'polo'), 6, 'hello', ['lemon', 'python']]

    六、列表的函数方法

    1.clear()  清楚列表元素

    ❤a = my_list.clear()的返回值是None

    my_list = [2,1,3]
    my_list.clear()
    print(my_list)      # []
    print(my_list.clear())   #None
    

      

    2.sort()  排序  数字排序,文字不需要排序

    my_list = [2,1,3,4,6,5]
    my_list.sort()
    print(my_list)      # [1,2,3,4,5,6]
    print(my_list.sort())   #None
    my_list = [1,2,3,['3',6]]
    my_list.sort()    #报错,不同类型是不能比较的

    3.reverse()降序

    my_list = [2,1,3,4,6,5]
    my_list.sort(reverse=True)
    print(my_list)      # [6,5,4,3,2,1]
    print(my_list.sort(reverse = True))  #None
  • 相关阅读:
    Linux查看程序端口占用情况
    详解大端模式和小端模式
    HDFS之二:HDFS文件系统JavaAPI接口
    HBase之四--(1):Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
    HBase之七:事务和并发控制机制原理
    HBase源码分析:HTable put过程
    QueryPerformanceFrequency使用方法--Windows高精度定时计数
    mongoDB的基本使用----飞天博客
    Android使用DOM生成和输出XML格式数据
    黑马程序猿_Java 代理机制学习总结
  • 原文地址:https://www.cnblogs.com/ananmy/p/12732567.html
Copyright © 2011-2022 走看看