Python串列 .reverse()反轉 .sort()排序 .remove(“指定元素”)移除 .pop(index)移除, 字串.replace(old,new) .lower()小寫 .upper()大寫 .title()首字大寫

加入好友
加入社群
Python串列 .reverse()反轉 .sort()排序 .remove("指定元素")移除 .pop(index)移除, 字串.replace(old,new) .lower()小寫 .upper()大寫 .title()首字大寫 - 儲蓄保險王

#Python串列 .reverse() .sort() .remove() .pop()

list2=[1,2,3,10,5]

#list是Python語法,盡量不要做變數名稱

#所以用list1, list2

print(list2.reverse())
#.reverse()本身傳回空值
print(list2)
# 已經將原數列反轉

print(list2.sort())
#.sort()本身傳回空值
print(list2)
# 已經將原數列排序

print(list2.remove(1))
#.remomve本身傳回空值
print(list2)
# 原數列已經刪除1

print(list2.pop(0))
# .pop本身傳回被刪除的元素
print(list2)
# 原數列已經刪除2

“””
以上都很容易誤會
會傳回反轉/排序/刪除後的數列
需回去找原數列

TQC 708 辭典合併dict1.update(dict2)也是一樣

dict3 = dict1.update(dict2)

dict3會是空值None

要找的仍是dict1

“””

Python串列 .reverse()反轉 .sort()排序 .remove("指定元素")移除 .pop(index)移除, 字串.replace(old,new) .lower()小寫 .upper()大寫 .title()首字大寫 - 儲蓄保險王

 

list中若有兩個以上的相同元素

一次remove()只能移除一個

可用以下語法:

Python串列 .reverse()反轉 .sort()排序 .remove("指定元素")移除 .pop(index)移除, 字串.replace(old,new) .lower()小寫 .upper()大寫 .title()首字大寫 - 儲蓄保險王

“””

但是string.replace(old,new)

就真的跟以上不同

回傳的是replace後的資料

“””

Python串列 .reverse()反轉 .sort()排序 .remove("指定元素")移除 .pop(index)移除, 字串.replace(old,new) .lower()小寫 .upper()大寫 .title()首字大寫 - 儲蓄保險王

 

“””

字串類的 .命令,都是回傳

執行命令後的結果

list , dict的 .命令

常回傳None

要回去找原list, dict

“””

Python串列 .reverse()反轉 .sort()排序 .remove("指定元素")移除 .pop(index)移除, 字串.replace(old,new) .lower()小寫 .upper()大寫 .title()首字大寫 - 儲蓄保險王

 

#再示範一次list.remove():

Python串列 .reverse()反轉 .sort()排序 .remove("指定元素")移除 .pop(index)移除, 字串.replace(old,new) .lower()小寫 .upper()大寫 .title()首字大寫 - 儲蓄保險王

 

加入好友
加入社群
Python串列 .reverse()反轉 .sort()排序 .remove("指定元素")移除 .pop(index)移除, 字串.replace(old,new) .lower()小寫 .upper()大寫 .title()首字大寫 - 儲蓄保險王

儲蓄保險王

儲蓄險是板主最喜愛的儲蓄工具,最喜愛的投資理財工具則是ETF,最喜愛的省錢工具則是信用卡

You may also like...

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *