測試環境為 CentOS 8 (虛擬機)
找出 List 物件中重複的字串,參考文章 – https://www.trainingint.com/how-to-find-duplicates-in-a-python-list.html .
[root@localhost ~]# python3 Python 3.6.8 (default, Sep 10 2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)] on linux Type "help", "copyright", "credits" or "license" for more information.
程式
mylist = [5, 3, 5, 2, 1, 6, 6, 4] newlist = [] duplist = [] for i in mylist: if i not in newlist: newlist.append(i) else: duplist.append(i)
執行結果
>>> duplist [5, 6]
>>> newlist [5, 3, 2, 1, 6, 4]
沒有解決問題,試試搜尋本站其他內容