Python基础题 - 4
看代码写结果:
1. a=[1,2,3,6,"dfs",100]
s=a[-1:]
print (s)
结果:[100]
2. s=a[-1:0:-1]
print(s)
结果:[100, “dfs”, 6, 3, 2]
1. 写代码,有如下列表,按照要求实现每一个功能
li = ["alex","wusir","eric","rain","alex"]
(1)计算列表的长度并输出
li = ["alex","wusir","eric","rain","alex"]
print(len(li))
结果:5
(2)列表中追加元素’seven’,并输出添加后的列表
li = ["alex","wusir","eric","rain","alex"]
li.append('seven')
print(li)
结果:['alex', 'wusir', 'eric', 'rain', 'alex', 'seven']
(3)请在列表的第1个位置插入元素’Tony’,并输出添加后的列表
li = ["alex","wusir","eric","rain","alex"]
li.insert(0,'Tony')
print(li)
结果:['Tony', 'alex', 'wusir', 'eric', 'rain', 'alex']
(4)请修改列表第2个位置的元素为’Kelly’,并输出修改后的列表
li = ["alex","wusir","eric","rain","alex"]
li[1] = 'Kelly'
print(li)
结果:['alex', 'Kelly', 'eric', 'rain', 'alex']
(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)
结果:['alex', 'wusir', 'eric', 'rain', 'alex', 1, 'a', 3, 4, 'heart']
(6)请将字符串s = ‘qwert’的每一个元素添加到列表li中,一行代码实现,不允许循环添加。
li = ["alex","wusir","eric","rain","alex"]
s = 'qwert'
li.extend(s)
print(li)
结果:['alex', 'wusir', 'eric', 'rain', 'alex', 'q', 'w', 'e', 'r', 't']
(7)请删除列表中的元素’eric’,并输出添加后的列表。
li = ["alex","wusir","eric","rain","alex"]
li.remove('eric')
print(li)
结果:['alex', 'wusir', 'rain', 'alex']
(8)请删除列表中的第2个元素,并输出删除的元素和删除元素后的列表。
li = ["alex","wusir","eric","rain","alex"]
print(li.pop(1))
print(li)
结果:wusir
['alex', 'eric', 'rain', 'alex']
(9)请删除列表中的第2至4个元素,并输出删除元素后的列表。
li = ["alex","wusir","eric","rain","alex"]
del li[1:4]
print(li)
结果:['alex', 'alex']
(10)请将列表所有的元素反转,并输出反转后的列表。
li = ["alex","wusir","eric","rain","alex"]
li.reverse()
print(li)
结果:['alex', 'rain', 'eric', 'wusir', 'alex']
(11)请计算出‘alex’元素在列表li中出现的次数,并输出该次数。
li = ["alex","wusir","eric","rain","alex"]
print(li.count('alex'))
结果:2
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"]
l1 = li[:3]
print(l1)
(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 = []
l5.append(li[-1])
print(l5)
(6)通过对li列表的切片形成新的列表l6,l6 = [‘b’,’a’,3]
li = [1,3,2,"a",4,"b",5,"c"]
l6 = li[-3:0:-2]
print(l6)
3,写代码,有如下列表,按照要求实现每一个功能。
lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],"ab","adv"]
(1)将列表lis中的’tt’变成大写(用两种方式)。
第一种方法:
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)
结果:[2, 3, 'k', ['qwe', 20, ['k1', ['TT', 3, '1']], 89], 'ab', 'adv']
第二种方法:
lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],"ab","adv"]
lis[3][2][1][0] = 'TT'
print(lis)
结果:[2, 3, 'k', ['qwe', 20, ['k1', ['TT', 3, '1']], 89], 'ab', 'adv']
(2)将列表中的数字3变成字符串’100’(用两种方式)。
第一种方法:
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, '100', 'k', ['qwe', 20, ['k1', ['tt', '100', '1']], 89], 'ab', 'adv']
第二种方法:
lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],"ab","adv"]
lis[1] =str(lis[1] + 97)
lis[3][2][1][1] =str(lis[3][2][1][1] + 97)
print(lis)
结果:[2, '100', 'k', ['qwe', 20, ['k1', ['tt', '100', '1']], 89], 'ab', 'adv']
(3)将列表中的字符串’1’变成数字101(用两种方式)。
第一种方法:
lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],"ab","adv"]
lis[3][2][1][2] =int(lis[3][2][1][2]) + 100
print(lis)
结果:[2, 3, 'k', ['qwe', 20, ['k1', ['tt', 3, 101]], 89], 'ab', 'adv']
第二种方法:
lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],"ab","adv"]
lis[3][2][1][2] = 101
print(lis)
结果:[2, 3, 'k', ['qwe', 20, ['k1', ['tt', 3, 101]], 89], 'ab', 'adv']
4. 请用代码实现:
li = ['alex','eric','rain']
利用下划线将列表的每一个元素拼接成字符串"alex_eric_rain"
li = ['alex','eric','rain']
li = '_'.join(li)
print(li)
5. 查找列表li中的元素,移除每个元素的空格,并找出以’A’或者’a’开头,
并以’c’结尾的所有元素,并添加到一个新列表中,最后循环打印这个新列表。
li = ["taibai ","alexC","AbC ","egon","Ritian"," Wusir"," agc"]
li = ["taibai ","alexC","AbC ","egon","Ritian"," Wusir"," agc"]
l1 = []
for i in li:
i = i.strip()
if (i.startswith('A') or i.startswith('a')) and i.endswith('c'):
l1.append(i)
print(l1)
6. 开发敏感词语过滤程序,提示用户输入评论内容,如果用户输入的内容中包含特殊的字符:
敏感词列表 li = ["苍老师","东京热",”武藤兰”,”波多野结衣”]
则将用户输入的内容中的敏感词汇替换成***,并添加到一个空列表中;如果用户输入的内容没有敏感词汇,则直接添加到上述的列表中。
li = ['苍老师', '东京热', '武藤兰', '波多野结衣']
l1 = [ ]
count = 0
i = 0
info = input('请输入内容:')
while count < len(li):
s = info.find(li[i])
if s != -1:
info = info.replace(li[i],'***')
count += 1
i += 1
else:
count += 1
i += 1
l1.append(info)
print(l1)
7. 有如下列表:循环打印列表中的每个元素,遇到列表则再循环打印出它里面的元素。(用两种方法实现,其中一种用range做)
li = [1, 3, 4, 'alex', [3, 7, 8, 'taibai'], 5, 'ritian']
第一种方法:
li = [1, 3, 4, 'alex', [3, 7, 8, 'taibai'], 5, 'ritian']
for i in li:
if type(i)==list:
for j in i:
print(j)
else:
print(i)
第二种方法:
li = [1, 3, 4, 'alex', [3, 7, 8, 'taibai'], 5, 'ritian']
for i in range(0, len(li)):
if type(li[i]) == list:
for j in range(0, len(li[i])):
print(li[i][j])
else:
print(li[i])
明日默写内容
1. 将列表的增删改查不同的方法全部写出来,
例如:增:有三种,append:在后面添加。Insert按照索引添加,expend:迭代着添加。