7

我有两个列表,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)

方法一:

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

方法二:

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
4

3 回答 3

7

使用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 数组,这是有道理的。

于 2013-08-21T04:58:03.267 回答
5

你没有正确计时

%timeit foo.sort()

在第一个循环之后,它已经对剩余部分进行了排序。Timsort 对于预排序列表非常有效。

我有点惊讶@Roman 使用键功能的速度如此之快。您可以通过使用进一步改进这一点itemgetter

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

这比对 1000 个元素的列表使用 lambda 函数快约 9%

于 2013-08-21T05:02:17.737 回答
4
>>> 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)

Performance:

>>> 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

To see the full picture:

>>> 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 method - fastest for 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

As @AshwiniChaudhary suggested in comments, for lists there's a way to speed it up by using itertools.izip instead of 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
于 2013-08-21T04:49:40.083 回答