python学习之老男孩python全栈第九期_day004作业

时间:2022-09-22 13:53:50

看代码写结果:
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:迭代着添加。

python学习之老男孩python全栈第九期_day004作业的更多相关文章

  1. python学习之老男孩python全栈第九期&lowbar;day002作业

    1. 判断下列逻辑语句的True,False.(1) 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6Tru ...

  2. python学习之老男孩python全栈第九期&lowbar;day015作业&lowbar;老男孩Python全9期练习题&lpar;面试真题模拟&rpar;

    一. 选择题(32分) 1. python不支持的数据类型有:AA. charB. intC. floatD. list 2. Ex = ‘foo’y = 2print(x + y)A. fooB. ...

  3. python学习之老男孩python全栈第九期&lowbar;day007作业

    一.关系运算 有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合pythons={'alex','egon','yuanhao','wu ...

  4. python学习之老男孩python全栈第九期&lowbar;day016作业

    1. 请利用filter()过滤出1~100中平方根是整数的数,即结果应该是: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] import math def func( ...

  5. python学习之老男孩python全栈第九期&lowbar;day001作业

    1.使用while循环输入 1 2 3 4 5 6     8 9 10 count = 0 while count <= 9: count += 1 if count == 7:continu ...

  6. python学习之老男孩python全栈第九期&lowbar;day003作业

    1. 有变量name = "aleX leNb" 完成如下操作:(1) 移除 name 变量对应的值两边的空格,并输出处理结果name = ' aleX leNb 'print(n ...

  7. python学习之老男孩python全栈第九期&lowbar;day014作业

    0. 默写a. 生成器函数获取移动平均值例子: def init(func): def inner(*args,**kwargs): ret = func(*args,**kwargs) ret.__ ...

  8. python学习之老男孩python全栈第九期&lowbar;day011作业

    1. 编写函数.(函数执行的时间是随机的) import timeimport randomdef random_time(): ''' 执行时间随机的函数 :return: ''' time.sle ...

  9. python学习之老男孩python全栈第九期&lowbar;day008作业

    1. 文件a.txt内容:每一行内容分别为商品名字,价钱,个数,求出本次购物花费的总钱数apple 10 3tesla 100000 1mac 3000 2lenovo 30000 3chicken ...

随机推荐

  1. windows系统调用 利用事件对象实现进程通信

    #include "iostream" #include "windows.h" #include "cstring" using name ...

  2. 张艾迪(创始人):Hello&period;世界&period;&period;&period;

    The World No.1 Girl :Eidyzhang The World No.1 Internet Girl :Eidyzhang AOOOiA.global Founder :Eidyzh ...

  3. &lbrack;转&rsqb;WPF&sol;MVVM快速开始手册

    I will quickly introduce some topics, then show an example that explains or demonstrates each point. ...

  4. npm reset config

    To reset user defaults Run this in the command line (or git bash on windows): echo "" > ...

  5. C编译环境的搭建-sublime篇

    1.安装sublime text 2.下载MingW: http://sourceforge.net/projects/mingw-w64/?source=typ_redirect 3.sublime ...

  6. leetcode day6 -- String to Integer &lpar;atoi&rpar; &amp&semi;amp&semi;&amp&semi;amp&semi; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  7. svn import usage and not import &period;so file

    svn import usage: svn import PATH URL 可以不检查仓库来实现.入文件或目录 for example: svn import /home/scott/xxx svn: ...

  8. 使用jquery的方法和技巧2,点击多选框的jquery响应

    使用jquery来控制多选框的变化 功能描述: 1.第一层 当选中后台应用(App1)时,所有多选框都被选择. 当取消选中后台应用(App1)时,所有多选框都被取消选择. 第一层的逻辑如下: 2.第二 ...

  9. 算法训练 K好数 数位DP&plus;同余定理

    思路:d(i,j)表示以i开头,长度为j的K好数的个数,转移方程就是 for(int u = 0; u < k; ++u) { int x = abs(i - u); if(x == 1) co ...

  10. Django 笔记(三)模版路径 ~ 静态引用

    1.模版路径: 在 settings,py 里的 TEMPLATES = [] 内添加一句代码拼接路径 'DIRS': [os.path.join(BASE_DIR, 'templates')] 有两 ...