所謂基礎不牢,地動山搖,咱們基礎學完了,但是要溫故而知新,

涉及知識點
- python 遍歷串列
- python 集合 set
- python 方法呼叫
代碼決議
咱們通過遍歷和集合兩個方式來實作
首先匯入使用的模塊
import platform
# Python學習交流群:279199867 # 進群后可領取海量:Python視頻教程、100本Python電子書、基礎、爬蟲、資料分析、web開發、機器學習、人工智能、面試題、Python學習路線圖、問題解答!點擊有道云筆記即可領取
畫蛇添足一下
print("俱往矣,數風流人物,還看今朝")
移除串列中的重復元素
輸入資料
input_list = [1, 2, 2, 3, 3, 3] print("輸入資料: ", input_list)
方法1: 遍歷串列
def method_1(): print("方法 1 : 遍歷串列") result = [] for e in input_list: if e not in result: result.append(e) print("結果: ", result)
方法2: 使用集合 set
def method_2(): print("方法 2 : 使用集合 set") result = list(set(input_list)) print("結果: ", result)
全部代碼
import platform print("俱往矣,數風流人物,還看今朝") input_list = [1, 2, 2, 3, 3, 3] print("輸入資料: ", input_list) def method_1(): print("方法 1 : 遍歷串列") result = [] for e in input_list: if e not in result: result.append(e) print("結果: ", result) def method_2(): print("方法 2 : 使用集合 set") result = list(set(input_list)) print("結果: ", result) method_1() method_2() print("Python 版本", platform.python_version())
運行結果

兄弟們快去試試吧!
來都來了,點個贊再走唄!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/499623.html
標籤:Python
