<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scienceoss.com &#187; NumPy</title>
	<atom:link href="http://scienceoss.com/tags/numpy/feed/" rel="self" type="application/rss+xml" />
	<link>http://scienceoss.com</link>
	<description>useful tidbits for using open source software in science</description>
	<lastBuildDate>Wed, 26 May 2010 03:34:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Sort one list by another list</title>
		<link>http://scienceoss.com/sort-one-list-by-another-list/</link>
		<comments>http://scienceoss.com/sort-one-list-by-another-list/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 14:14:26 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[NumPy]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[argsort]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://scienceoss.com/sort-one-list-by-another-list/</guid>
		<description><![CDATA[Here are a couple of ways of sorting one list by another list in Python. The first uses plain ol&#8217; Python, and the others use NumPy. In each case imagine we want to sort a list of peoples names by their ages. Method 1 Zip the lists together, making sure that the one to sort [...]]]></description>
			<content:encoded><![CDATA[<p>Here are a couple of ways of sorting one list by another list in Python.  The first uses plain ol&#8217; Python, and the others use NumPy.</p>
<p>In each case imagine we want to sort a list of peoples names by their ages.<span id="more-108"></span></p>
<h3>Method 1</h3>
<p>Zip the lists together, making sure that the one to sort by is passed first to zip().  The result is a list of tuples.  When you sort a list of tuples, it sorts using the first item in each tuple.  Then use the zip* trick to unzip the now sorted tuples into separate variables.</p>
<pre class="brush: python; title: ; notranslate">people = ['Jim', 'Pam', 'Micheal', 'Dwight']
ages = [27, 25, 4, 9]

agesAndPeople = zip(ages, people)
agesAndPeople.sort()
sortedAges, sortedPeople = zip(*agesAndPeople)</pre>
<p>Note that if you want to sort in reverse, you can use <span class="c">agesAndPeople.sort(reverse=True)</span>.</p>
<h3>Method 2</h3>
<p>This method uses NumPy, and you don&#8217;t have to convert the lists into arrays.  The argsort() function doesn&#8217;t return the sorted ages . . . instead, it returns the indices that each item would if it were in an already sorted array (try it to see what I mean).  take() is a way of using useful NumPy indexing on a list.  See the next example for something that might be more straigtforward for Matlab users.</p>
<pre class="brush: python; title: ; notranslate">people = ['Jim', 'Pam', 'Micheal', 'Dwight']
ages = [27, 25, 4, 9]

import numpy
inds = numpy.argsort(ages)
sortedPeople = numpy.take(people, inds)</pre>
<h3>Method 3</h3>
<p>This method also uses NumPy, but first it converts the lists into arrays.  Then it uses the argsort() of one to index into the other.</p>
<pre class="brush: python; title: ; notranslate">people = ['Jim', 'Pam', 'Micheal', 'Dwight']
ages = [27, 25, 4, 9]

import numpy
people = numpy.array(people)
ages = numpy.array(ages)
inds = ages.argsort()
sortedPeople = people[inds]</pre>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/sort-one-list-by-another-list/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

