列表类型内置方法
1、定义:[]内可以有多个任意类型的值,逗号分隔元素
2、用途:表示多个爱好,多个人名,地名等
my_best_friend = ['jike','kang','sheng']print(f'my_best_friend:{my_best_friend}')my_best_friend:['jike', 'kang', 'sheng'] l = list ('hello kangkang')print(f'l:{l}')l:['h', 'e', 'l', 'l', 'o', ' ', 'k', 'a', 'n', 'g', 'k', 'a', 'n', 'g']
3.常用操作+内置方法:常用操作和内置方法分为优先掌握(今天必须得记住)、需要掌握(一周内记住)、其他操作(了解)三个部分。
1.按索引取值(正向取值+反向取值),即可存也可以取
my_best_friend[0]='jike beautiful'print(f'my_best_friend[0]:{my_best_friend[0]}')my_best_friend[0]:jike beautiful
2.切片
name_list =['king','tom','bob','jim','jack','mike','nacy','sat']#索引序号 0 1 2 3 4 5 6 7print(f'{name_list[3:]}') #切片从序号3一直截取到最后一位,默认步长为1print(f'{name_list[2:7]}') #切片从序号2一直截取到序号6,默认步长为1print(f'{name_list[2:7:2]}') #切片从序号2截取到序号6,步长为2print(f'{name_list[2::2]}') #切片从序号2一直截取到最后一位,步长为2'''结果是:['jim', 'jack', 'mike', 'nacy', 'sat']['bob', 'jim', 'jack', 'mike', 'nacy']['bob', 'jack', 'nacy']['bob', 'jack', 'nacy']'''#注意:步长也可以为负,当步长为负时,索引从右向左走#逆向思维:当输入的索引序号为负数时,就是指从右往左数的位置,规定最后一位为-1print(f'{name_list[:]}') #切片所有,即取出整个列表的值,因为默认步长为1print(f'{name_list[::-1]}') #反向切片,反向取所有即从右往左取出整个列表的值print(f'{name_list[-3:-1:1]}') #切片从-3截取到-2,正向截取print(f'{name_list[-2:-8:-1]}') #切片从-2截取到-7,反向截取'''结果是:['king', 'tom', 'bob', 'jim', 'jack', 'mike', 'nacy', 'sat']['sat', 'nacy', 'mike', 'jack', 'jim', 'bob', 'tom', 'king']['mike', 'nacy']['nacy', 'mike', 'jack', 'jim', 'bob', 'tom']
3.len(表示列表的长度)
name_list = ['qwefv','dfgb','qwwv','zxcd','mjuih','wetug','mkouy','vbnedg']print(len(name_list))print(len(name_list))print(len(name_list))888
4.成员运算in和not in
name_list = ['qwefv','dfgb','qwwv','zxcd','mjuih','wetug','mkouy','vbnedg']print(f"'bnhu' not in name_list:{'bnhu' not in name_list}")print(f"'bfgh' in name_list: {'bfgh'in name_list}")'bnhu' not in name_list:True'bfgh' in name_list: False
5.追加值(append)
name_list = ['qwefv','dfgb','qwwv','zxcd','mjuih','wetug','mkouy','vbnedg']name_list.append('zcvnn loiub') #追加是在列表后面追加print(name_list)['qwefv', 'dfgb', 'qwwv', 'zxcd', 'mjuih', 'wetug', 'mkouy', 'vbnedg', 'zcvnn loiub']
6.删除(del)
name_list = ['qwefv','dfgb','qwwv','zxcd','mjuih','wetug','mkouy','vbnedg']del name_list[5] #按索引删除值print(name_list)['qwefv', 'dfgb', 'qwwv', 'zxcd', 'mjuih', 'mkouy', 'vbnedg']
7.循环(list循环)
name_list = ['qwesc', 'zxcsd', 'mkou', 'bdgyh', 'mkihgg', 'mkdfia', 'nmjigf']for i in range(len(name_list)): print(i,name_list[i])0 qwesc1 zxcsd2 mkou3 bdgyh4 mkihgg5 mkdfia6 nmjigf
必须掌握(****)
1.insert()
name_list = ['qwesc','zxcsd','mkou','bdgyh','mkihgg','mkdfia','nmjigf']name_list.insert(4,'band') #在索引前面加入一个值print(name_list)['qwesc', 'zxcsd', 'mkou', 'bdgyh', 'band', 'mkihgg', 'mkdfia', 'nmjigf']
2.pop()默认删除选中的序号
name_list = ['qwesc','zxcsd','mkou','bdgyh','mkihgg','mkdfia','nmjigf']print(f'name_list.pop(4):{name_list.pop(4)}') #默认删除最后一个元素,删除指定索引的值print(name_list)['qwesc', 'zxcsd', 'mkou', 'bdgyh', 'mkdfia', 'nmjigf']
3.remove()删除所选中的元素
name_list = ['qwesc','zxcsd','mkou','bdgyh','mkihgg','mkdfia','nmjigf']print(f"name_list.remove('mkou'):{name_list.remove('mkou')}") #如果删除对象不存在则报错,按照选定的元素进行移除print(name_list)name_list.remove('mkou'):None['qwesc', 'zxcsd', 'bdgyh', 'mkihgg', 'mkdfia', 'nmjigf']
4.count()计数 及列表中出现所选中元素的次数
name_list = ['qwesc','zxcsd','mkou','mkou','bdgyh','mkihgg','mkdfia','nmjigf']print(f"name_list.count('mkou'):{name_list.count('mkou')}") #获取选定元素的个数name_list.count('mkou'):2
5.index()索引值
name_list = ['qwesc','zxcsd','mkou','bdgyh','mkihgg','mkdfia','nmjigf']print(f"name_list.index('mkou'):{name_list.index('mkou')}") #获取选定元素的索引,只会获取第一次的name_list.index('mkou'):2
6.clear()清除
name_list = ['qwesc','zxcsd','mkou','bdgyh','mkihgg','mkdfia','nmjigf']name_list.clear() #clear 清除列表所有元素print(f'name_list:{name_list}') #f-string格式化name_list:[]
7.copy()复制
name_list = ['qwesc','zxcsd','mkou','bdgyh','mkihgg','mkdfia','nmjigf']print(f'name_list.copy():{name_list.copy()}')name_list.copy():['qwesc', 'zxcsd', 'mkou', 'bdgyh', 'mkihgg', 'mkdfia', 'nmjigf']
8.extend()用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
name_list = ['qwesc','zxcsd','mkou','mkou','bdgyh','mkihgg','mkdfia','nmjigf']name_list2 = ['kang']name_list.extend(name_list2) #将extend里面的列表元素添加到原列表print(f'name_list:{name_list}')name_list:['qwesc', 'zxcsd', 'mkou', 'mkou', 'bdgyh', 'mkihgg', 'mkdfia', 'nmjigf', 'kang']
9.reverse()用于反向列表中元素 即把列表中的元素颠倒回来.
name_list = ['qwesc','zxcsd','mkou','mkou','bdgyh','mkihgg','mkdfia','nmjigf']name_list.reverse() #作用把列表中的元素倒置过来print(f'name_list:{name_list}')name_list:['nmjigf', 'mkdfia', 'mkihgg', 'bdgyh', 'mkou', 'mkou', 'zxcsd', 'qwesc']
10.sort()排序
list.sort(cmp=None, key=None, reverse=False)
- cmp -- 可选参数, 如果指定了该参数会使用该参数的方法进行排序。
- key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
- reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)
name_list = ['qwesc','zxcsd','mkou','mkou','bdgyh','mkihgg','mkdfia','nmjigf']name_list.sort() #sort()对列表中所有元素进行排序print(f"name_list:{name_list}")name_list.sort(reverse = True) #reverse()对以上进行排序的列表进行反向排序print(f"name_list1:{name_list}")name_list:['bdgyh', 'mkdfia', 'mkihgg', 'mkou', 'mkou', 'nmjigf', 'qwesc', 'zxcsd']name_list1:['zxcsd', 'qwesc', 'nmjigf', 'mkou', 'mkou', 'mkihgg', 'mkdfia', 'bdgyh']
4、存一个值or多个值:多个值
5、有序or无序:有序(有索引,可以按照索引取值)
6、可变与不可变:可变数据类型,(当变量值发生改变,id内存地址不变)
name_list = ['stone','rock','river','xian']print(id(name_list))name_list.append('sun')print(id(name_list))68991526899152