|

Sorting Two Lists Together According to The Order of One List in Python

We may use two lists to store the values of two properties of a list of elements in Python. Under such data structure arrangement, when we need to sort the properties from one list, we may want to also make sure the other list will be also re-ordered following the same order as the first list.

For example, we may have two lists containing the x and y coordinates of points.

x = [3, 5, 6, 1, 2]
y = [6, 7, 8, 9, 10]

If we sort the points by their x coordinates, we want to sort the lists x and y together according to the orders of values in the list x.

TL;DR zip-sort-unzip

To sort 2 lists x and y by x:

new_x, new_y = zip(*sorted(zip(x, y)))

Functions used to sort two lists together

We can achieve this by using 3 built-in functions: zip(list1, list2), sorted(list) and zip(*list).

zip(list1, list2) returns a zip object by pairing the elements at the same positions from both lists. For the above example, zip(x, y) will be

>>> for elem in zip(x, y):
...     print(elem)
... 
(3, 6)
(5, 7)
(6, 8)
(1, 9)
(2, 10)

sorted(list) returns the sorted list. For the above zip object containing the list of paired values, the sorted() list will be as follows.

>>> for elem in sorted(zip(x, y)):
...     print(elem)
... 
(1, 9)
(2, 10)
(3, 6)
(5, 7)
(6, 8)

The values are now sorted, but paired. We can then use zip(*list) to decouple the list of paired elements to 2 lists.

>>> for elem in zip(*sorted(zip(x, y))):
...     print(elem)
... 
(1, 2, 3, 5, 6)
(9, 10, 6, 7, 8)

Python code to sort two lists according to one list

Combining them together, we can order the 2 lists together by

new_x, new_y = zip(*sorted(zip(x, y)))

For the above example, the new_x and new_y will be:

>>> new_x, new_y = zip(*sorted(zip(x, y)))
>>> new_x
(1, 2, 3, 5, 6)
>>> new_y
(9, 10, 6, 7, 8)

Now we sorted both lists together.

Similar Posts

  • Gnome2 Style: Shiki Colors+Gnome Colors

    The Linux Gnome style that I like is Shiki-color + Gnome colors icon theme. It looks pretty and professional and it is fast. How to install them: Gtk engine: # yum install gtk-murrine-engine Icon theme: # yum install gnome-colors-icon-theme Theme: Dowload from here: http://gnome-look.org/content/show.php/Shiki-Colors?content=86717 Install it by Appearance Preference -> Install. Configuration: Appearance Preference ->…

  • SetProxy: 一个设置IE代理的命令行小工具

    IE的代理设置用起来并不方便,我自己而言就要经常更改代理服务器,相信很多人有跟我相同的需要。使用C++编写了一个小程序SetProxy调用Win32 API来设置代理服务器IP,用起来方便多了。 编译后为一个可运行console程序,将它放在\windows\system32目录下,使用时只要运行 SetProxy IP:port 就可以把IP设置好,如果要关闭代理,只需运行 SetProxy “” 命令行中运行,界面较土,但用着方便。可以考虑设置几个快捷方式在桌面或者工具栏,点击即设置代理,其它方式发挥想象。 程序下载地址. 源代码也放在这里,希望有需要的人可以用得上 ;) 源代码下载地址. 这是一份有些年头的代码, 如果你在较新的编译器下编译这个项目, 请先做[[setproxy-一个设置ie代理的小工具#comment-995|这里所述的小修改]]. — Eric on Apr. 9, 2014. Read more: Making Emacs Start Up Faster Profiling Vim to Find Out Which Plugin Makes Vim Slow Handling Sparse Files on Linux Spring Shell Technology For Java development Vim Tutorial for Beginners: vimtutor…

  • How to export and import message filters of Thunderbird

    I have several installation of Thunderbird under Linux to check/send emails using IMAP email servers. I use filters extensively to manage my emails. How to export and import message filters of Thunderbird so that I can synchronize the rules on several installations? About message filters in Thunderbird (More at http://kb.mozillazine.org/Filters_(Thunderbird)): Filters are account specific, there…

Leave a Reply

Your email address will not be published. Required fields are marked *