[ python ] 練習做業 - 1

1,寫代碼,有以下列表,按照要求實現每個功能api

li = ["alex","wusir","eric","rain","alex"]

 

 

1)計算列表的長度並輸出app

len(li)

 

 

2)列表中追加元素’seven’,並輸出添加後的列表spa

li = ["alex","wusir","eric","rain","alex"]
li.append('seven')
print(li)

 

 

3)請在列表的第1個位置插入元素’Tony’,並輸出添加後的列表code

li = ["alex","wusir","eric","rain","alex"]
li.insert(0, 'Tony')
print(li)

 

 

4)請修改列表第2個位置的元素爲’Kelly’,並輸出修改後的列表blog

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中,一行代碼實現,不容許循環添加。ip

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個元素,並輸出刪除的元素和刪除元素後的列表rem

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)

 

 

六、開發敏感詞語過濾程序,提示用戶輸入評論內容,若是用戶輸入的內容中包含特殊的字符:敏感詞列表 li = ["蒼老師","東京熱",」武藤蘭」,」波多野結衣」]則將用戶輸入的內容中的敏感詞彙替換成***.

li = ["蒼老師","東京熱","武藤蘭","波多野結衣"]
while True:
    str1 = input('>>>').strip()
    if str1 in li:
        str1 = str1.replace(str1, '***')
    print(str1)

 

 

七、有以下列表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(): 插入,根據索引進行插入
相關文章
相關標籤/搜索