学习内容:列表类型内置方法

列表类型内置方法

list(掌握)

  1. 用途:多个装备、多个爱好、多门课程。
  2. 定义:[]内用逗号分隔的多个任意类型的值。
    like = ['chenglong','zhenzidan','lilianjie']
    print(f'like:{like}')
    
    
    like:['chenglong', 'zhenzidan', 'lilianjie']
    n = list('hello jin')
    print(f'n:{n}')
    
    
    n:['h', 'e', 'l', 'l', 'o', ' ', 'j', 'i', 'n']
  3. 常用操作加内置方法:
    优先掌握:

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

    name_list = ['lol','dnf','cf','qq']
    print(f'name_list:{name}')
    
    
    name_list:['lol', 'dnf', 'cf', 'qq']

    2.切片['索引值':]

    gamename_list = ['lol','dnf','cf','qq']
    print(f'gamename:[0:3:2]:{gamename_list[0:3:2]}')
    
    
    gamename:[0:3:2]:['lol', 'cf']

    3.长度len

    gamename_list = ['lol','dnf','cf','qq']
    print(f'len(gamename_list):{len(gamename_list)}')
    
    
    len(gamename_list):4

    4.成员运算in和not in

    gamename_list = ['lol','dnf','cf','qq']
    print(f"'wzry' in gamename_list:{'wzry' in gamename_list}")
    print(f"'wzry' in gamename_list:{'wzry' not in gamename_list}")
    
    
    'wzry' in gamename_list:False
    'wzry' in gamename_list:True

    5.追加值append

    gamename_list = ['lol','dnf','cf','qq']
    gamename_list.append('wzry')
    
    
    gamename_list:['lol', 'dnf', 'cf', 'qq', 'wzry']

    6.删除del

    gamename_list = ['lol','dnf','cf','qq']
    del gamename_list[3]
    print(f'del gamename_list{gamename_list}')
    
    
    del gamename_list['lol', 'dnf', 'cf']

    7.循环 for i in

    for name in gamename_list :
        print(name)
    
    
    lol
    dnf
    cf
    qq
    需要掌握

    1.insert 在索引前面插入一个值

    gamename_list = ['lol','dnf','cf','qq']
    gamename_list.insert(0,'wx')
    print(f'gamename_list:{gamename_list}')
    
    
    
    gamename_list:['wx', 'lol', 'dnf', 'cf', 'qq']

    2.pop() 删除指定索引的值,默认就是删除最后一个值

    gamename_list = ['lol','dnf','cf','qq']
    print(f'gamename_list.pop:{gamename_list.pop()}')
    print(f'gamename_list.pop:{gamename_list.pop()}')
    
    
    gamename_list.pop:qq
    gamename_list.pop:cf

    3.remove 按照指定的元素删除,如果删除的元素不存在则会报错

    gamename_list = ['lol','dnf','cf','qq']
    print(f"gamename_list.remove(lol):{gamename_list.remove('lol')}")
    print(f'gamename_list.remove(lol):{gamename_list}')
    
    
    
    gamename_list.remove(lol):None
    gamename_list.remove(lol):['dnf', 'cf', 'qq']

    4.count() 获取指定元素的个数

    gamename_list = ['lol','dnf','cf','qq']
    print(f"gamename_list.count('lol'):{gamename_list.count('lol')}")
    
    
    gamename_list.count('lol'):1

    5.index() 获取指定元素的索引,但是只会获取第一次

    gamename_list = ['lol','dnf','cf','qq']
    print(f"gamename_list.index('dnf'):{gamename_list.index('dnf')}")
    
    
    gamename_list.index('dnf'):1

    6.clear() 清除列表

    gamename_list = ['lol','dnf','cf','qq']
    gamename_list.clear()
    print(f"gamename_list.clear():{gamename_list}")
    
    
    
    gamename_list.clear():[]

    7.copy() 复制

    gamename_list = ['lol','dnf','cf','qq']
    print(f'gamename_list.copy():{gamename_list.copy()}')
    
    
    gamename_list.copy():['lol', 'dnf', 'cf', 'qq']

    8.extend() 扩展,把extend里的列表的元素添加到原件列表中

    gamename_list = ['lol','dnf','cf','qq']
    gamename_list2 = ['wzry','kp']
    gamename_list.extend(gamename_list2)
    print(f'gamename_listg:{gamename_list}')
    
    
    gamename_listg:['lol', 'dnf', 'cf', 'qq', 'wzry', 'kp']

    9.reverse 反转列表

    gamename_list = ['lol','dnf','cf','qq']
    gamename_list.reverse()
    print(f'gamename_list:{gamename_list}')
    
    
    
    gamename_list:['qq', 'cf', 'dnf', 'lol']

    10.sort() 排序列表 sort列表的元素必须是同类型

    gamename_list = ['lol','dnf','cf','qq']
    gamename_list.sort()
    print(f'gamename_list:{gamename_list}')
    
    gamename_list.sort(reverse = True)
    print(f'gamename_list:{gamename_list}')
    
    
    
    gamename_list:['cf', 'dnf', 'lol', 'qq']
    gamename_list:['qq', 'lol', 'dnf', 'cf']

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

    5.有序or无序:有序

    hobby_lise = ['run','read','play']
    print(f'first:{id(hobby_lise)}')
    hobby_lise[2] = ''
    print(f'second:{id(hobby_lise)}')
    
    
    first:35407816
    second:35407816

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

猜你喜欢

转载自www.cnblogs.com/jincoco/p/10923607.html
0条评论
添加一条新回复