百万年薪python之路 -- 列表练习

时间:2021-05-04 15:16:55

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

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
计算列表的长度并输出

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
print(len(li))

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

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
li.append("seven")
print(li)

请在列表的第2个位置前插入元素"Tony",并输出添加后的列表

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
li.insert(1,"Tony")
print(li)

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

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
li[1] = "Kelly"
print(li)

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

li.extend(l2)

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

li.extend(s)

请删除列表中的元素"ritian",并输出添加后的列表

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
li.pop(2)
print(li)

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

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
d = li.pop(1)
print("删除的元素:",d)
print("删除后的列表:",li)

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

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
del li[1:4]
print(li)

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

li = [1, 3, 2, "a", 4, "b", 5,"c",["a","b","cc"]]
通过对li列表的切片形成新的列表l1,l1 = [1,3,2]

li = [1, 3, 2, "a", 4, "b", 5,"c",["a","b","cc"]]
l1 = li[:3]
print(l1)

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

li = [1, 3, 2, "a", 4, "b", 5,"c",["a","b","cc"]]
l2 = li[3:6]
print(l2)

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

li = [1, 3, 2, "a", 4, "b", 5,"c",["a","b","cc"]]
l3 = li[0:7:2]
print(l3)

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

li = [1, 3, 2, "a", 4, "b", 5,"c",["a","b","cc"]]
l4 = li[1:6:2]
print(l4)

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

li = [1, 3, 2, "a", 4, "b", 5,"c",["a","b","cc"]]
l5 = list(li[-2])
print(l5)

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

li = [1, 3, 2, "a", 4, "b", 5,"c",["a","b","cc"]]
l6 = li[-4:-9:-2]
print(l6)

通过对li列表的切片形成新的列表l7,l7 = ['cc', 'b', 'a']

li = [1, 3, 2, "a", 4, "b", 5,"c",["a","b","cc"]]
l7 = li[-1][::-1]
print(l7)

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

lis = [2, 33, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
将列表lis中的"tt"变成大写(用两种方式)。

lis = [2, 33, "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, 33, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
lis[3][2][1][0] = "TT"
print(lis)

将列表中的数字3变成字符串"100"(用两种方式)。

lis = [2, 33, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
lis[3][2][1][1] = "100"
print(lis)

lis = [2, 33, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
lis[3][2][1][1] = str(100)
print(lis)

lis = [2, 33, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
lis[3][2][1].remove(3)
lis[3][2][1].insert(1,"100")
print(lis)

将列表中的字符串"1"变成数字101(用两种方式)。

lis = [2, 33, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
lis[3][2][1][-1] = 101
print(lis)

lis = [2, 33, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
lis[3][2][1][-1] = int(lis[3][2][1][-1].replace("1","101"))
print(lis)

lis = [2, 33, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
lis[3][2][1].remove("1")
lis[3][2][1].append("100")
print(lis)

4.请用代码实现:
li = ["alex", "wusir", "taibai"]
利用下划线将列表的每一个元素拼接成字符串"alex_wusir_taibai"

li = ["alex", "wusir", "taibai"]
s = "_".join(li)
print(s)

5.利用for循环和range打印出下面列中每个元素的索引。

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

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
for i in range(len(li)):
    print(i)

6.利用for循环和range将100以内所有的偶数添加到一个新列表中。

lst = []
for i in range(0,100):
    if i % 2 == 0:
        lst.append(i)
print(lst)

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

lst = []
for i in range(0,50):
    if i % 3 == 0:
        lst.append(i)
print(lst)

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

for i in range(100,-2,-1):
    print(i)

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

lst = []
for i in range(100,9,-1):
    if i % 2 == 0:
        lst.append(i)
for i in lst:
    if i % 4 != 0:
        lst.remove(i)
print(lst)

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

lst = []
for i in range(1,31):
    if i % 3 == 0:
        lst.append("*")
        continue
    lst.append(i)
print(lst)

11.查找列表li中的元素,移除每个元素的空格,并找出以"A"或者"a"开头,并以"c"结尾的所有元素,并添加到一个新列表中,最后循环打印这个新列表。
li = ["TaiBai ", "alexC", "AbC ", "egon", " riTiAn", "WuSir", " aqc"]

lst = []
li = ["TaiBai ", "alexC", "AbC ", "egon", " riTiAn", "WuSir", " aqc"]
for i in li:
    i = i.replace(" ","")
    if (i.startswith("A") or i.startswith("a")) and i.endswith("c"):
        lst.append(i)
for i in lst:
    print(i)

12.开发敏感词语过滤程序,提示用户输入评论内容,如果用户输入的内容中包含特殊的字符:
敏感词列表 li = ["苍老师", "东京热", "武藤兰", "波多野结衣"]
则将用户输入的内容中的敏感词汇替换成等长度的(苍老师就替换** *),并添加到一个列表中;如果用户输入的内容没有敏感词汇,则直接添加到上述的列表中。

lst = []
li = ["苍老师", "东京热", "武藤兰", "波多野结衣"]
while 1:
    i = 0
    inp = input("请输入内容(退出按q或Q):")
    if inp.lower() == "q":
        break
    # if inp in li:i
    for i in li:
        if i in inp:
            inp = inp.replace(i,"*" * len(i))
    lst.append(inp)
print(lst)

13.有如下列表(选做题)
li = [1, 3, 4, "alex", [3, 7, 8, "TaiBai"], 5, "RiTiAn"]
循环打印列表中的每个元素,遇到列表则再循环打印出它里面的元素。
我想要的结果是:
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) == str:
        i = i.lower()
    if type(i) == list:
        for j in i:
            if type(j) == str:
                j = j.lower()
            print(j)
    else:
        print(i)

14.用户输入一个数字,使用列表输出这个数字内的斐波那契数列,如下列表:(选做题)
用户输入100 输出[1,1,2,3,5,8,13,21,34,55,89]这个列表

a = 1
b = 1
sum_num = 0
lst = []
inp = int(input("请输入数字:"))
while sum_num <= inp:
    if a == 1:
        sum_num = b + a
        lst.append(a)
        lst.append(b)
        lst.append(sum_num)
    temp = sum_num
    if (sum_num + a) > inp:
        break
    sum_num += a
    a = temp
    lst.append(sum_num)
print(lst)