1,写代码,有如下列表,按照要求实现每一个功能
li = ["alex","wusir","eric","rain","alex"]
1)计算列表的长度并输出
len(li)
2)列表中追加元素’seven’,并输出添加后的列表
li = ["alex","wusir","eric","rain","alex"] li.append('seven') print(li)
3)请在列表的第1个位置插入元素’Tony’,并输出添加后的列表
li = ["alex","wusir","eric","rain","alex"] li.insert(0, 'Tony') print(li)
4)请修改列表第2个位置的元素为’Kelly’,并输出修改后的列表
li = ["alex","wusir","eric","rain","alex"] li.insert(1, 'Kelly') print(li)
5)请将列表l2=[1,’a’,3,4,’heart’]的每一个元素添加到列表li中,一行代码实现,不允许循环添加。
li = ["alex", "wusir", "eric", "rain", "alex"] l2 = [1, 'a', 3, 4, 'heart'] li.extend(l2) print(li)
6)请将字符串s = ‘qwert’的每一个元素添加到列表li中,一行代码实现,不允许循环添加。
li = ["alex", "wusir", "eric", "rain", "alex"] s = 'qwert' li.extend(s) print(li)
7)请删除列表中的元素’eric’,并输出添加后的列表
li = ["alex", "wusir", "eric", "rain", "alex"] li.remove('eric') print(li)
8)请删除列表中的第2个元素,并输出删除的元素和删除元素后的列表
li = ["alex", "wusir", "eric", "rain", "alex"] li.pop(2) print(li)
9)请删除列表中的第2至4个元素,并输出删除元素后的列表
li = ["alex", "wusir", "eric", "rain", "alex"] del li[2:4] print(li)
10)请将列表所有得元素反转,并输出反转后的列表
li = ["alex", "wusir", "eric", "rain", "alex"] reversed(li) print(li)
11)请计算出‘alex’元素在列表li中出现的次数,并输出该次数。
li = ["alex", "wusir", "eric", "rain", "alex"] print(li.count('alex'))
2,写代码,有如下列表,利用切片实现每一个功能
li = [1,3,2,"a",4,"b",5,"c"]
1)通过对li列表的切片形成新的列表l1,l1 = [1,3,2]
li = [1,3,2,"a",4,"b",5,"c"] print(li[:3])
2)通过对li列表的切片形成新的列表l2,l2 = [’a’,4,’b’]
li = [1,3,2,"a",4,"b",5,"c"] l2 = li[3:6] print(l2)
3)通过对li列表的切片形成新的列表l3,l3 = [’1,2,4,5]
li = [1,3,2,"a",4,"b",5,"c"] l3 = li[::2] print(l3)
4)通过对li列表的切片形成新的列表l4,l4 = [3,’a’,’b’]
li = [1,3,2,"a",4,"b",5,"c"] l4=li[1:6:2] print(l4)
5)通过对li列表的切片形成新的列表l5,l5 = [‘c’]
li = [1,3,2,"a",4,"b",5,"c"] l5 = li[-1:] print(l5)
6)通过对li列表的切片形成新的列表l6,l6 = [‘b’,’a’,3]
li = [1, 3, 2, "a", 4, "b", 5, "c"] l6 = reversed(li[1:6:2]) print(list(l6))
3,写代码,有如下列表,按照要求实现每一个功能。
lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],"ab","adv"]
1)将列表lis中的’tt’变成大写(用两种方式)。
(1)第一种方法: 利用索引及upper() lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],"ab","adv"] lis[3][2][1][0] = lis[3][2][1][0].upper() print(lis) lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],"ab","adv"] lis[3][2][1][0] = lis[3][2][1][0].replace('tt', 'TT') print(lis) (2)第二种方法:利用插入和删除 lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],"ab","adv"] lis[3][2][1].insert(0, 'TT') lis[3][2][1].pop(1) print(lis)
2)将列表中的数字3变成字符串’100’(用两种方式)。
(1)第一种方法:利用索引 lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],"ab","adv"] lis[1] = 100 lis[3][2][1][1] = 100 print(lis) (2)第二种方法:利用插入和删除 lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],"ab","adv"] lis.insert(1, 100) lis.pop(2) lis[3][2][1].insert(1, 100) lis[3][2][1].pop(2) print(lis)
3)将列表中的字符串’1’变成数字101(用两种方式)。
(1)第一种方法:利用索引 lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],"ab","adv"] lis[3][2][1][-1] = 101 print(lis) (2)第二种方法:利用插入和删除 lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],"ab","adv"] lis[3][2][1].insert(3, 101) print(lis) lis[3][2][1].pop(-2) print(lis)
4,请用代码实现:
li = ["alex","eric","rain"]
利用下划线将列表的每一个元素拼接成字符串"alex_eric_rain"
li = ["alex","eric","rain"] print("_".join(li))
5,查找列表li中的元素,移除每个元素的空格,并找出以’A’或者’a’开头,并以’c’结尾的所有元素,并添加到一个新列表中,最后循环打印这个新列表。
li = ["taibai ","alexC","AbC ","egon","Ritian"," Wusir"," agc"] l2 = [] for k in li: k = k.strip() if k.capitalize().startswith('A') and k.endswith('c'): l2.append(k) print(l2)
6、开发敏感词语过滤程序,提示用户输入评论内容,如果用户输入的内容中包含特殊的字符:敏感词列表 li = ["苍老师","东京热",”武藤兰”,”波多野结衣”]则将用户输入的内容中的敏感词汇替换成***.
li = ["苍老师","东京热","武藤兰","波多野结衣"] while True: str1 = input('>>>').strip() if str1 in li: str1 = str1.replace(str1, '***') print(str1)
7、有如下列表li = [1,3,4’,alex’,[3,7,8,’taibai’],5,’ritian’]循环打印列表中的每个元素,遇到列表则再循环打印出它里面的元素。
li = [1,3,4, 'xiaom', [3,7,8, [5,6,7,8], 'taibai'],5,'hotdog'] def func(lis): for i in lis: if isinstance(i, list): return func(i) print(i) func(li)
将列表的增删改查不同的方法全部写出来
list.append(): 新增,默认在末尾追加元素
list.extend(): 迭代添加元素(熟记)
list.pop(): 删除,默认删除最后一个元素
list.insert(): 插入,根据索引进行插入