|

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

  • OCaml Learning Materials

    OCaml is an interesting functional language. There are lots learning materials on the Internet. I compile a list of resources for OCaml learning and reference. Recommended OCaml learning and reference material Online book of Real World OCaml by Yaron Minsky, Anil Madhavapeddy, Jason Hickey. A very good tutorial by Jason Hickey: http://www.cs.caltech.edu/courses/cs134/cs134b/book.pdf. The OCaml system…

  • Transactional memory learning materials

    I want to learn transactional memory technologies. Any suggestions on Transactional memory learning materials? Thanks! I highly suggest the Transactional Memory lecture by James R. Larus and Ravi Rajwar of Synthesis Lectures on Computer Architecture: The Transactional Memory lecture:http://www.morganclaypool.com/doi/abs/10.2200/S00070ED1V01Y200611CAC002 Link to the PDF:http://www.morganclaypool.com/doi/pdf/10.2200/S00070ED1V01Y200611CAC002 Read more: Mmaping Memory Range Larger Than the Total Size of Physical…

  • Alternatives to goto in bash

    As we know: There is no goto statement support in bash. goto is useful for certain situations, especially for debugging. So, is there any alternative mechanisms to goto in bash? Robert Copeland gave an interesting hacking to this problem: http://bobcopeland.com/blog/2012/10/goto-in-bash/ The idea is like the self-modifying code. But the difference is to generate a piece…

  • How to force a checkpointing of metadata in HDFS?

    HDFS SecondaraNameNode log shows 2017-08-06 10:54:14,488 ERROR org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode: Exception in doCheckpoint java.io.IOException: Inconsistent checkpoint fields. LV = -63 namespaceID = 1920275013 cTime = 0 ; clusterId = CID-f38880ba-3415-4277-8abf-b5c2848b7a63 ; blockpoolId = BP-578888813-10.6.1.2-1497278556180. Expecting respectively: -63; 263120692; 0; CID-d22222fd-e28a-4b2d-bd2a-f60e1f0ad1b1; BP-622207878-10.6.1.2-1497242227638. at org.apache.hadoop.hdfs.server.namenode.CheckpointSignature.validateStorageInfo(CheckpointSignature.java:134) at org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode.doCheckpoint(SecondaryNameNode.java:531) at org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode.doWork(SecondaryNameNode.java:395) at org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode$1.run(SecondaryNameNode.java:361) at org.apache.hadoop.security.SecurityUtil.doAsLoginUserOrFatal(SecurityUtil.java:415) at org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode.run(SecondaryNameNode.java:357) It seems the checkpoint…

  • Where is the source code for the free command on Linux?

    Where can I find the source code for the free command on Linux? The source code for the free commands (and many more, like kill, ps, top, sysctl) can be found in procps-ng: https://gitlab.com/procps-ng/procps procps is a set of command line and full-screen utilities that provide information out of the pseudo-filesystem most commonly located at…

Leave a Reply

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