簡體   English   中英

排序多個列表的最快方法 - Python

[英]Fastest way to sort multiple lists - Python

我有兩個列表,x和y,我想通過x排序的排列來排序x和置換y。 例如,給定

x = [4, 2, 1, 3]
y = [40, 200, 1, 30]

我想得到

x_sorted = [1,2,3,4]
y_sorted = [1, 200, 30, 40]

正如過去的問題所討論的,解決這個問題的一個簡單方法是

x_sorted, y_sorted = zip(*sorted(zip(x,y)))

這是我的問題:最快的方法是什么?


我有三種方法來完成任務。

import numpy as np
x = np.random.random(1000)
y = np.random.random(1000)

方法1:

x_sorted, y_sorted = zip(*sorted(zip(x,y))) #1.08 ms 

方法2:

foo = zip(x,y)
foo.sort()
zip(*foo)       #1.05 ms

方法3;

ind = range(1000)
ind.sort(key=lambda i:x[i])
x_sorted = [x[i] for i in ind]
y_sorted = [y[i] for i in ind]  #934us

有沒有比上述三種方法更快的執行方法?


其他問題。

  1. 為什么方法2不比方法1快,盡管它使用排序方法?
  2. 如果我單獨執行方法2,它會更快。 在IPython終端中,

我有

%timeit foo = zip(x,y)   #1000 loops, best of 3: 220 us per loop
%timeit foo.sort()       #10000 loops, best of 3: 78.9 us per loop
%timeit zip(*foo)        #10000 loops, best of 3: 73.8 us per loop

使用numpy.argsort

>>> import numpy as np
>>> x = np.array([4,2,1,3])
>>> y = np.array([40,200,1,30])
>>> order = np.argsort(x)
>>> x_sorted = x[order]
>>> y_sorted = y[order]
>>> x_sorted
array([1, 2, 3, 4])
>>> y_sorted
array([  1, 200,  30,  40])

>>> timeit('order = np.argsort(x); x_sorted = x[order]; y_sorted = y[order]', 'from __main__ import x, y, np', number=1000)
0.030632019043

注意

如果輸入數據已經是numpy數組,這是有意義的。

>>> x = [4, 2, 1, 3]
>>> y = [40, 200, 1, 30]    
>>> x_sorted, y_sorted = zip(*sorted(zip(x, y), key=lambda a:a[0]))
>>> x_sorted
(1, 2, 3, 4)
>>> y_sorted
(1, 200, 30, 40)

性能:

>>> timeit('foo = zip(x,y); foo.sort(); zip(*foo)', 'from __main__ import x, y', number=1000)
1.0197240443760691
>>> timeit('zip(*sorted(zip(x,y)))', 'from __main__ import x, y', number=1000)
1.0106219310922597
>>> timeit('ind = range(1000); ind.sort(key=lambda i:x[i]); x_sorted = [x[i] for i in ind]; y_sorteds = [y[i] for i in ind]', 'from __main__ import x, y', number=1000)
0.9043525504607857
>>> timeit('zip(*sorted(zip(x, y), key=lambda a:a[0]))', 'from __main__ import x, y', number=1000)
0.8288150863453723

要查看完整圖片:

>>> timeit('sorted(x)', 'from __main__ import x, y', number=1000)
0.40415491505723367            # just getting sorted list from x
>>> timeit('x.sort()', 'from __main__ import x, y', number=1000)
0.008009909448446706           # sort x inplace

@falsetru方法 - 對於np.arrays來說速度最快

>>> timeit('order = np.argsort(x); x_sorted = x[order]; y_sorted = y[order]', 'from __main__ import x, y, np', number=1000)
0.05441799872323827

正如@AshwiniChaudhary在評論中所建議的那樣, 對於列表,有一種方法可以通過使用itertools.izip而不是zip來加速它:

>>> timeit('zip(*sorted(izip(x, y), key=itemgetter(0)))', 'from __main__ import x, y;from operator import itemgetter;from itertools import izip', number=1000)
0.4265049757161705

你沒有正確計時

%timeit foo.sort()

在第一個循環之后,它已經為剩余部分排序了。 Timsort對預先排序的列表非常有效。

我有點驚訝@ Roman使用關鍵功能的速度要快得多。 您可以使用itemgetter進一步改進

from operator import itemgetter
ig0 = itemgetter(0)
zip(*sorted(zip(x, y), key=ig0))

這比使用lambda函數對1000個元素的列表快9%

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM