• 列表类型内置方法


    列表类型内置方法

    一、列表类型内置方法(list)

    1.用途:多个装备、多个爱好、多门课程,甚至是多个女朋友

    2.定义:[]内可以有多个任意类型的值,逗号分隔元素

    Copy# my_girl_friend = list(['jason','tank','sean'])
    my_girl_friend = ['jason', 'tank', 'sean']
    
    print(f"my_girl_friend: {my_girl_friend}")
    Copymy_girl_friend: ['jason', 'tank', 'sean']
    Copyl = list('hello nick')
    print(f"l: {l}")
    Copyl: ['h', 'e', 'l', 'l', 'o', ' ', 'n', 'i', 'c', 'k']
    

    3.常用操作+内置方法:常用操作和内置方法分为优先掌握(今天必须得记住)、需要掌握(一周内记住)、其他操作(了解)三个部分。

    1.1 优先掌握(*****)

    1. 按索引取值(正向取值+反向取值),即可存也可以取
    2. 切片
    3. 长度len
    4. 成员运算in和not in
    5. 追加append
    6. 删除del
    7. 循环

    1.按索引取值(正向取值+反向取值),即可存也可以取

    Copy# list之索引取值
    name_list = ['nick', 'jason', 'tank', 'sean']
    name_list[0] = 'nick handsom'
    # name_list[1000] = 'tank sb'  # 报错
    
    print(f"name_list[0]: {name_list[0]}")
    Copyname_list[0]: nick handsom
    

    2.切片

    Copy# list之切片
    name_list = ['nick', 'jason', 'tank', 'sean']
    
    print(f"name_list[0:3:2]: {name_list[0:3:2]}")
    Copyname_list[0:3:2]: ['nick', 'tank']
    

    3.长度

    Copy# list之长度
    name_list = ['nick', 'jason', 'tank', 'sean']
    
    print(f"len(name_list): {len(name_list)}")
    Copylen(name_list): 4
    

    4.成员运算in和not in

    Copy# list之成员运算in和not in
    name_list = ['nick', 'jason', 'tank', 'sean']
    
    print(f"'tank sb' in name_list: {'tank sb' in name_list}")
    print(f"'nick handsome' not in name_list: {'nick handsome' not in name_list}")
    Copy'tank sb' in name_list: False
    'nick handsome' not in name_list: True
    

    5.追加值

    Copy# list之追加值
    name_list = ['nick', 'jason', 'tank', 'sean']
    name_list.append('tank sb')
    
    print(f"name_list: {name_list}")
    Copyname_list: ['nick', 'jason', 'tank', 'sean', 'tank sb']
    

    6.删除

    Copy# list之删除
    name_list = ['nick', 'jason', 'tank', 'sean']
    del name_list[2]
    
    print(f"name_list: {name_list}")
    Copyname_list: ['nick', 'jason', 'sean']
    

    7.循环

    Copy# list之循环
    name_list = ['nick', 'jason', 'tank', 'sean']
    
    for name in name_list:
        print(name)
    Copynick
    jason
    tank
    sean
    

    1.2 需要掌握(****)

    1. insert
    2. pop
    3. remove
    4. count
    5. index
    6. clear
    7. copy
    8. extend
    9. reverse
    10. sort

    1.insert()

    Copy# list之insert()
    name_list = ['nick', 'jason', 'tank', 'sean']
    name_list.insert(1, 'handsome')
    
    print(f"name_list: {name_list}")
    Copyname_list: ['nick', 'handsome', 'jason', 'tank', 'sean']
    
    

    2.pop()

    Copy# list之pop(),pop()默认删除最后一个元素
    name_list = ['nick', 'jason', 'tank', 'sean']
    
    print(f"name_list.pop(1): {name_list.pop(1)}")
    print(f"name_list: {name_list}")
    Copyname_list.pop(1): jason
    name_list: ['nick', 'tank', 'sean']
    
    

    3.remove()

    Copy# list之remove()
    name_list = ['nick', 'jason', 'tank', 'sean']
    
    print(f"name_list.remove('nick'): {name_list.remove('nick')}")
    print(f"name_list: {name_list}")
    Copyname_list.remove('nick'): None
    name_list: ['jason', 'tank', 'sean']
    
    

    4.count()

    Copy# list之count()
    name_list = ['nick', 'jason', 'tank', 'sean']
    
    print(f"name_list.count('nick'): {name_list.count('nick')}")
    Copyname_list.count('nick'): 1
    
    

    5.index()

    Copy# list之index()
    name_list = ['nick', 'jason', 'tank', 'sean']
    
    print(f"name_list.index('nick'): {name_list.index('nick')}")
    Copyname_list.index('nick'): 0
    
    

    6.clear()

    Copy# list之clear()
    name_list = ['nick', 'jason', 'tank', 'sean']
    name_list.clear()
    
    print(f"name_list: {name_list}")
    Copyname_list: []
    
    

    7.copy()

    Copy# list之copy()
    name_list = ['nick', 'jason', 'tank', 'sean']
    
    print(f"name_list.copy(): {name_list.copy()}")
    Copyname_list.copy(): ['nick', 'jason', 'tank', 'sean']
    
    

    8.extend()

    Copy# list之extend()
    name_list = ['nick', 'jason', 'tank', 'sean']
    name_list2 = ['nick handsome']
    name_list.extend(name_list2)
    
    print(f"name_list: {name_list}")
    Copyname_list: ['nick', 'jason', 'tank', 'sean', 'nick handsome']
    
    

    9.reverse()

    Copy# list之reverse()
    name_list = ['nick', 'jason', 'tank', 'sean']
    name_list.reverse()
    
    print(f"name_list: {name_list}")
    Copyname_list: ['sean', 'tank', 'jason', 'nick']
    

    10.sort()

    Copy# list之sort(),使用sort列表的元素必须是同类型的
    name_list = ['nick', 'jason', 'tank', 'sean']
    name_list.sort()
    
    print(f"name_list: {name_list}")
    
    name_list.sort(reverse=True)
    print(f"name_list_reverse: {name_list}")
    Copyname_list: ['jason', 'nick', 'sean', 'tank']
    name_list_reverse: ['tank', 'sean', 'nick', 'jason']
    

    4.存一个值or多个值:多个值

    5.有序or无序:有序

    Copyhobby_list = ['read', 'run', 'girl']
    print(f'first:{id(hobby_list)}')
    hobby_list[2] = ''
    print(f'second:{id(hobby_list)}')
    Copyfirst:4522187016
    second:4522187016
    

    6.可变or不可变:可变数据类型

    我把月亮戳到天上 天就是我的 我把脚踩入地里 地就是我的 我亲吻你 你就是我的
  • 相关阅读:
    EF-CodeFirst-3搞事
    EF-CodeFirst-1 玩起来
    EF-CodeFirst-2玩的嗨
    Asp.Net SignalR Hub类中的操作详解
    Asp.Net SignalR GlobalHost外部通知
    Asp.Net SignalR 多平台的Client与Server
    Asp.Net SignalR 集群会遇到的问题
    常用数学符号读法大全
    关于神经网络拟合任意函数的讨论
    Asp.net MVC使用FormsAuthentication,MVC和WEB API可以共享身份认证 (转载)
  • 原文地址:https://www.cnblogs.com/zhulipeng-1998/p/12863824.html
Copyright © 2020-2023  润新知