python之道05

python

1.写代码,有如下列表,按照要求实现每一个功能

python">li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]

​ 1)计算列表的长度并输出

​ 答案:

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]

print(len(li))

'''

输出结果

5

'''

​ 2)列表中追加元素"seven", 并输出添加后的列表

​ 答案:

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]

li.append('seven')

print(li)

'''

输出结果

['alex', 'WuSir', 'ritian', 'barry', 'wenzhou', 'seven']

'''

​ 3)请在列表的第1个位置插入元素"Tony", 并输出添加后的列表

答案:

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]

li.insert(0,'Tony')

print(li)

'''

输出结果

['Tony', 'alex', 'WuSir', 'ritian', 'barry', 'wenzhou']

'''

​ 4)请修改列表第2个位置的元素为"Kelly", 并输出修改后的列表

​ 答案:

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]

li[1] = 'Kelly'

print(li)

'''

输出结果

['alex', 'Kelly', 'ritian', 'barry', 'wenzhou']

'''

​ 5)请将列表l2 = [1, "a", 3, 4, "heart"]的每一个元素添加到列表li中,一行代码实现,不允许循环添加。

​ 答案:

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]

l2 = [1, "a", 3, 4, "heart"]

li = li + l2

print(li)

'''

输出结果

['alex', 'WuSir', 'ritian', 'barry', 'wenzhou', 1, 'a', 3, 4, 'heart']

'''

​ 6)请将字符串s = "qwert"的每一个元素添加到列表li中,一行代码实现,不允许循环添加。

​ 答案:

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]

s = "qwert"

li.extend(s)

print(li)

'''

输出结果

['alex', 'WuSir', 'ritian', 'barry', 'wenzhou', 'q', 'w', 'e', 'r', 't']

'''

​ 7)请删除列表中的元素"ritian", 并输出输出后的列表

​ 答案:

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]

li.remove('ritian')

print(li)

'''

输出结果

['alex', 'WuSir', 'barry', 'wenzhou']

'''

​ 8)请删除列表中的第2个元素,并输出删除的元素和删除元素后的列表

​ 答案:

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]

print(li.pop(1))

print(li)

'''

输出结果

['alex', 'ritian', 'barry', 'wenzhou']

'''

​ 9)请删除列表中的第2至4个元素,并输出删除元素后的列表

​ 答案:

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]

del li[1:4]

print(li)

'''

输出结果

['alex', 'wenzhou']

'''

​ 10)请将列表所有得元素反转,并输出反转后的列表

​ 答案:

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]

print(li[::-1])

'''

输出结果

['wenzhou', 'barry', 'ritian', 'WuSir', 'alex']

'''

# 方法二

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]

li.reverse()

print(li)

'''

输出结果

['wenzhou', 'barry', 'ritian', 'WuSir', 'alex']

'''

​ 11)请计算出"alex"元素在列表li中出现的次数,并输出该次数。

​ 答案:

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]

print(li.count('alex'))

'''

输出结果

1

'''

2.写代码,有如下列表,利用切片实现每一个功能

​ 1)通过对li列表的切片形成新的列表l1, l1 = [1, 3, 2]

​ 答案:

li = [1, 3, 2, "a", 4, "b", 5, "c"]

l1 = li[:3]

print(l1)

'''

输出结果

[1, 3, 2]

'''

​ 2)通过对li列表的切片形成新的列表l2, l2 = ["a", 4, "b"]

​ 答案:

li = [1, 3, 2, "a", 4, "b", 5, "c"]

l2 = li[3:6]

print(l2)

'''

输出结果

['a', 4, 'b']

'''

​ 3)通过对li列表的切片形成新的列表l3, l3 = ["1,2,4,5]

​ 答案:

li = [1, 3, 2, "a", 4, "b", 5, "c"]

l3 = li[::2]

print(l3)

'''

输出结果

[1, 2, 4, 5]

'''

​ 4)通过对li列表的切片形成新的列表l4, l4 = [3, "a", "b"]

​ 答案:

li = [1, 3, 2, "a", 4, "b", 5, "c"]

l4 = li[1:-2:2]

print(l4)

'''

输出结果

[3, 'a', 'b']

'''

​ 5)通过对li列表的切片形成新的列表l5, l5 = ["c"]

​ 答案:

li = [1, 3, 2, "a", 4, "b", 5, "c"]

l5 = li[-1:]

print(l5)

'''

输出结果

c

'''

​ 6)通过对li列表的切片形成新的列表l6, l6 = ["b", "a", 3]

​ 答案:

li = [1, 3, 2, "a", 4, "b", 5, "c"]

l6 = li[-3::-2]

print(l6)

'''

输出结果

['b', 'a', 3]

'''

3.写代码,有如下列表,按照要求实现每一个功能。

​ 1)将列表lis中的"tt"变成大写(用两种方式)

​ 答案:

# 方式一:

lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]

print(lis[3][2][1][0].upper())

'''

输出结果

TT

'''

# 方式二

lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]

lis[3][2][1][0] = 'TT'

print(lis)

'''

输出结果

TT

'''

​ 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] = 101

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] = int(lis[3][2][1][2]) + 100

print(lis)

'''

输出结果

[2, 3, 'k', ['qwe', 20, ['k1', ['tt', 3, 101]], 89], 'ab', 'adv']

'''

4.请用代码实现

li = ["alex", "wusir", "taibai"]

利用下划线将列表的每一个元素拼接成字符串"alex_wusir_taibai"

​ 答案:

li = ["alex", "wusir", "taibai"]

print(li[0]+'_'+li[1]+'_'+li[2])

'''

输出结果

alex_wusir_taibai

'''

5.利用for循环和range打印出下面列表的索引

li = ["alex", "WuSir", "ritian", "barry", "wenzho"]

​ 答案:

# 方式一:for循环 (这个理解错了题意,请看方式二)

li = ["alex", "WuSir", "ritian", "barry", "wenzho"]

count = -1

for i in li:

count += 1

print(count)

'''

输出结果

0

1

2

3

4

'''

# 方式二:for和range

li = ["alex", "WuSir", "ritian", "barry", "wenzho"]

for i in range(len(li)):

print(i)

'''

输出结果

0

1

2

3

4

'''

6.利用while循环打印出下面列表的索引

li = ["alex", "WuSir", "ritian", "barry", "wenzho"]

​ 答案:

li = ["alex", "WuSir", "ritian", "barry", "wenzho"]

count = 0

while count < len(li):

print(count)

count += 1

'''

输出结果

0

1

2

3

4

'''

7.利用for循环和range找出100以内所有的偶数并将这些偶数添加到一个新列表中。

​ 答案:

l1 = []

for i in range(100):

if i % 2 == 0:

l1.append(i)

print(l1)

'''

输出结果

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]

'''

8.利用for循环和range找出50以内能被3整除的数,并将这些数插入到一个新列表中。

​ 答案:

l1 = []

for i in range(50):

if i % 3 == 0:

l1.append(i)

print(l1)

'''

输出结果

[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]

'''

9.利用for循环和range从100~1,倒序打印

​ 答案:

li = []

for i in range(1,101):

li.append(i)

print(li[::-1])

'''

输出结果

[100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

'''

# 方式二

li = []

for i in range(1,101,-1):

li.append(i)

print(li)

'''

输出结果

[100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

'''

10.利用for循环和range打印100~10,倒序将所有的偶数添加到一个新列表中,然后在对列表的元素进行筛选,将能被4整除的数留下来。

​ 答案:

li = []

l2 = []

for i in range(10,101):

if i % 2 == 0:

li.append(i)

for l in li:

if l % 4 == 0:

l2.append(l)

print(l2)

'''

输出结果

[12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100]

'''

# 一行实现,不满足题意

li = []

for i in range(10,101):

if i % 4 == 0:

li.append(i)

print(li)

'''

输出结果

[12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100]

'''

# 方式三

11.利用for循环和range,将1-30的数字依次添加到一个列表中,并循环这个列表,将能被3整除的数改成*。

​ 答案:

li = []

for i in range(1,31):

li.append(i)

for l in range(len(li)):

if li[l] % 3 == 0:

li[l] = '*'

print(li)

'''

输出结果

[1, 2, '*', 4, 5, '*', 7, 8, '*', 10, 11, '*', 13, 14, '*', 16, 17, '*', 19, 20, '*', 22, 23, '*', 25, 26, '*', 28, 29, '*']

'''

12.查找列表li中的元素,移除每个元素的空格,并找出以"A"或者"a"开头,并以"c"结尾的所有元素,并添加到一个新列表中,最后循环打印这个新列表。

li = ["TaiBai ", "alexC", "AbC ", "egon", " riTiAn", "WuSir", " aqc"]

​ 答案:

l1 = []

li = ["TaiBai ", "alexC", "AbC ", "egon", " riTiAn", "WuSir", " aqc"]

for i in li:

j = i.strip()

if j[0].upper() == 'A' and j[-1] == 'c':

l1.append(j)

print(l1)

'''

输出结果

['aqc']

'''

13.开发敏感词语过滤程序,提示用户输入评论内容,如果用户输入的内容中包含特殊的字符:

敏感词列表 li = ["苍老师", "东京热", "武藤兰", "波多野结衣"]

则将用户输入的内容中的敏感词汇替换成等长度的(苍老师就替换**),并添加到一个列表中;如果用户输入的内容没有敏感词汇,则直接添加到上述的列表中。

​ 答案:

li = ["苍老师", "东京热", "武藤兰", "波多野结衣"]

l1 = []

while 1:

comment = input('请输出评论内容: ')

if comment in li:

l1.append('*' * len(comment))

else:

l1.append(comment)

print(l1)

'''

输出结果

请输出评论内容: 不上班

['老男孩', '郭德纲', '***', '***', '许三多', '不上班']

'''

# 以上这个有bug,如果输入: '苍老师你好',就无法转换成*

# 方式二

comment = input('请输出评论内容: ')

for i in comment:

if

14.有如下列表(选做题)

li = [1, 3, 4, "alex", [3, 7, 8, "BaoYuan"], 5, "RiTiAn"]

循环打印列表中的每个元素,遇到列表则再循环打印出它里面的元素。

我想要的结果是:

1

3

4

alex

3

7

8

baoyuan

5

ritian

​ 答案:

# 方式一

li = [1, 3, 4, "alex", [3, 7, 8, "BaoYuan"], 5, "RiTiAn"]

for i in li:

if type(i) == list:

for j in i:

if type(j) == str:

print(j.lower())

else:

print(j)

elif type(i) == str:

print(i.lower())

else:

print(i)

# 方式二

li = [1, 3, 4, "alex", [3, 7, 8, "BaoYuan"], 5, "RiTiAn"]

for i in li:

if type(i) == int:

print(i)

elif type(i) == str:

if i.lower() == "ritian":

print(i.lower())

else:

print('"' + str(i).lower() + '"')

else:

for j in i:

if j == 7:

print(str(j) + ',')

elif type(j) == int:

print(j)

else:

print('"' + str(j).lower() + '"')

'''

输出结果

1

3

4

"alex"

3

7,

8

"baoyuan"

5

ritian

'''

端午作业:

1.day4之前所有内容,进行笔记整理

2.整理day4之前的所有的知识点画思维导图

3.继续向后预习

以上是 python之道05 的全部内容, 来源链接: utcz.com/z/388992.html

回到顶部