列表去重是Python中一種常見(jiàn)的處理方式,任何編程場(chǎng)景都可能會(huì)遇到需要列表去重的情況。
列表去重的方式有很多,本文將一一講解他們,并進(jìn)行性能的對(duì)比。
讓我們先制造一些簡(jiǎn)單的數(shù)據(jù),生成0到99的100萬(wàn)個(gè)隨機(jī)數(shù):
from random import randrange
DUPLICATES = [randrange(100) for _ in range(1000000)]
接下來(lái)嘗試這4種去重方式中最簡(jiǎn)單直觀(guān)的方法:
1.新建一個(gè)數(shù)組,遍歷原數(shù)組,如果值不在新數(shù)組里便加入到新數(shù)組中。
# 第一種方式
def easy_way():
unique = []
for element in DUPLICATES:
if element not in unique:
unique.append(element)
return unique
進(jìn)入ipython使用timeit計(jì)算其去重耗時(shí):
%timeit easy_way()
# 1.16 s ± 137 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

平均耗時(shí)在1.16秒左右,但是在這個(gè)例子中我們使用了數(shù)組作為存儲(chǔ)對(duì)象,實(shí)際上如果我們改成集合存儲(chǔ)去重后的結(jié)果,性能會(huì)快不少:
def easy_way():
unique = set()
for element in DUPLICATES:
if element not in unique:
unique.add(element)
return unique
%timeit easy_way()
# 48.4 ms ± 11.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
平均耗時(shí)在48毫秒左右,改善明顯,這是因?yàn)榧虾蛿?shù)組的內(nèi)在數(shù)據(jù)結(jié)構(gòu)完全不同,集合使用了哈希表,因此速度會(huì)比列表快許多,但缺點(diǎn)在于無(wú)序。
接下來(lái)看看第2種方式:
2.直接對(duì)數(shù)組進(jìn)行集合轉(zhuǎn)化,然后再轉(zhuǎn)回?cái)?shù)組:
# 第二種去重方式
def fast_way()
return list(set(DUPLICATES))
耗時(shí):
%timeit fast_way()
# 14.2 ms ± 1.73 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)
平均耗時(shí)14毫秒,這種去重方式是最快的,但正如前面所說(shuō),集合是無(wú)序的,將數(shù)組轉(zhuǎn)為集合后再轉(zhuǎn)為列表,就失去了原有列表的順序。
如果現(xiàn)在有保留原數(shù)組順序的需要,那么這個(gè)方式是不可取的,怎么辦呢?
3.保留原有數(shù)組順序的去重
使用dict.fromkeys()函數(shù),可以保留原有數(shù)組的順序并去重:
def save_order():
return list(dict.fromkeys(DUPLICATES))
當(dāng)然,它會(huì)比單純用集合進(jìn)行去重的方式耗時(shí)稍微久一點(diǎn):
%timeit save_order()
# 39.5 ms ± 8.66 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
平均耗時(shí)在39.5毫秒,我認(rèn)為這是可以接受的耗時(shí),畢竟保留了原數(shù)組的順序。
但是,dict.fromkeys()僅在Python3.6及以上才支持。
如果你是Python3.6以下的版本,那么可能要考慮第四種方式了。
4. Python3.6以下的列表保留順序去重
在Python3.6以下,其實(shí)也存在fromkeys函數(shù),只不過(guò)它由collections提供:
from collections import OrderedDict
def save_order_below_py36():
return list(OrderedDict.fromkeys(DUPLICATES))
耗時(shí):
%timeit save_order_below_py36()
# 71.8 ms ± 16.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
平均耗時(shí)在72毫秒左右,比 Python3.6 的內(nèi)置dict.fromkeys()慢一些,因?yàn)?code>OrderedDict是用純Python實(shí)現(xiàn)的。
-
數(shù)據(jù)
+關(guān)注
關(guān)注
8文章
7309瀏覽量
93678 -
存儲(chǔ)
+關(guān)注
關(guān)注
13文章
4648瀏覽量
89315 -
python
+關(guān)注
關(guān)注
56文章
4850瀏覽量
89309 -
數(shù)組
+關(guān)注
關(guān)注
1文章
420瀏覽量
27069
發(fā)布評(píng)論請(qǐng)先 登錄
python八種連接列表的方式
Python 編程常用的12種基礎(chǔ)知識(shí)匯總
小猿圈python學(xué)習(xí)之Python列表list合并的4種方法
使用Python實(shí)現(xiàn)對(duì)excel文檔去重及求和的方法和代碼說(shuō)明
如何運(yùn)用Python列表的元素比較解決問(wèn)題

Python列表去重的4種方式
評(píng)論