Interesting list sorting in python


This isn’t really worthy of a blog post as it’s somewhat rudimentary and easily found right in the docs (though sort of indirectly), but it is something that was mostly overlooked by me until recently. The trick is is mylist.sort(key=...).

http://docs.python.org/tutorial/datastructures.html#more-on-lists
http://docs.python.org/library/functions.html#sorted


import random
from operator import attrgetter

class thing(object):
def __init__(self):
self.num=random.randint(1,9999)
def __repr__(self):
return ""%(self.num,self.count)

things = []
for count in range(1,25):
things.append(thing())
things[-1].count=count

things.sort(key=attrgetter('num'))

This will create and then sort the list as shown below (respectively):

[<(thing) num=3432, count=1>,
<(thing) num=1172, count=2>,
<(thing) num=5961, count=3>,
<(thing) num=3958, count=4>,
<(thing) num=4558, count=5>,
<(thing) num=7211, count=6>,
<(thing) num=244, count=7>,
<(thing) num=1766, count=8>,
<(thing) num=4296, count=9>,
<(thing) num=677, count=10>,
<(thing) num=8769, count=11>,
<(thing) num=5060, count=12>,
<(thing) num=8071, count=13>,
<(thing) num=9589, count=14>,
<(thing) num=3643, count=15>,
<(thing) num=2605, count=16>,
<(thing) num=3970, count=17>,
<(thing) num=3476, count=18>,
<(thing) num=6284, count=19>,
<(thing) num=374, count=20>,
<(thing) num=8685, count=21>,
<(thing) num=5909, count=22>,
<(thing) num=5770, count=23>,
<(thing) num=228, count=24>]


[<(thing) num=228, count=24>,
<(thing) num=244, count=7>,
<(thing) num=374, count=20>,
<(thing) num=677, count=10>,
<(thing) num=1172, count=2>,
<(thing) num=1766, count=8>,
<(thing) num=2605, count=16>,
<(thing) num=3432, count=1>,
<(thing) num=3476, count=18>,
<(thing) num=3643, count=15>,
<(thing) num=3958, count=4>,
<(thing) num=3970, count=17>,
<(thing) num=4296, count=9>,
<(thing) num=4558, count=5>,
<(thing) num=5060, count=12>,
<(thing) num=5770, count=23>,
<(thing) num=5909, count=22>,
<(thing) num=5961, count=3>,
<(thing) num=6284, count=19>,
<(thing) num=7211, count=6>,
<(thing) num=8071, count=13>,
<(thing) num=8685, count=21>,
<(thing) num=8769, count=11>,
<(thing) num=9589, count=14>]

Advertisements

About andyortlieb

I often find myself figuring out some niche oddities, only to find myself stuck on those same problems a year later due to my wide yet thin activity in certain topics related to my career and hobbies. This blog is where I document these nuances (or nuisances) to ease my pain the second time around, and hopefully that of some other fellow desperate internet scouts.
This entry was posted in Programming and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s