1. 程式人生 > >python中陣列去重

python中陣列去重

source = ['a','a','b','c','d','d']
dest = []
for e in source:
    if e not in dest:
        dest.append(e)
print(dest)

列印結果:
[‘a’, ‘b’, ‘c’, ‘d’]

迴圈遍歷原陣列,如果元素已經在目標陣列中(說明此元素已經重複),則不新增。如果沒有在目標陣列中(說明還未重複),則新增。