6
我有一個500萬字符串元素的列表,它們被存儲爲一個pickle對象。python list(set(a))每次都改變順序嗎?
a = ['https://en.wikipedia.org/wiki/Data_structure','https://en.wikipedia.org/wiki/Data_mining','https://en.wikipedia.org/wiki/Statistical_learning_theory','https://en.wikipedia.org/wiki/Machine_learning','https://en.wikipedia.org/wiki/Computer_science','https://en.wikipedia.org/wiki/Information_theory','https://en.wikipedia.org/wiki/Statistics','https://en.wikipedia.org/wiki/Mathematics','https://en.wikipedia.org/wiki/Signal_processing','https://en.wikipedia.org/wiki/Sorting_algorithm','https://en.wikipedia.org/wiki/Data_structure','https://en.wikipedia.org/wiki/Quicksort','https://en.wikipedia.org/wiki/Merge_sort','https://en.wikipedia.org/wiki/Heapsort','https://en.wikipedia.org/wiki/Insertion_sort','https://en.wikipedia.org/wiki/Introsort','https://en.wikipedia.org/wiki/Selection_sort','https://en.wikipedia.org/wiki/Timsort','https://en.wikipedia.org/wiki/Cubesort','https://en.wikipedia.org/wiki/Shellsort']
要刪除重複,我用set(a)
,然後我做了一個列表再通過list(set(a))
。
我的問題是:
即使我重新啓動蟒蛇,並宣讀了泡菜文件列表中,將在list(set(a))
的順序是一樣的每一次?
我很想知道這個散列 - >列表排序是如何工作的。
我測試了一個小的數據集,它似乎有一個一致的排序。
In [50]: a = ['x','y','z','k']
In [51]: a
['x', 'y', 'z', 'k']
In [52]: list(set(a))
['y', 'x', 'k', 'z']
In [53]: b=list(set(a))
In [54]: list(set(b))
['y', 'x', 'k', 'z']
In [55]: del b
In [56]: b=list(set(a))
In [57]: b
['y', 'x', 'k', 'z']
。 –
對於初學者來說,哈希的順序不能保證,所以列表的順序也不能保證。 – Makoto
我想你可以使用[ordered-set](https://pypi.python.org/pypi/ordered-set)而不是'set' – MaxU