[Tutor] Sorting 2-d data

Wayne srilyk at gmail.com
Sun Sep 13 15:18:00 CEST 2009


On Sun, Sep 13, 2009 at 7:27 AM, Rich Lovely <roadierich at googlemail.com>wrote:

> 2009/9/13 Lie Ryan <lie.1296 at gmail.com>:
> > Wayne wrote:
> >> <snip>
> > if your data is like this:
> > mylist = [
> >    [3, 2, 4, 1],
> >    [4, 1, 2, 3],
> >    [3, 1, 2, 1],
> >    [2, 1, 1, 1],
> >    [1, 1, 2, 1],
> > ]
> >
> > you can use zip(*mylist) to transform your data to row-first list, sort
> > using the above method, then zip(*mylist) again. Beware that zip(*mylist)
> > returns a list of tuples even if the original data is list of lists. I
> think
> > there should be an easier (and faster) way, that others can point out.
>

That zip method is very useful because my original data set is in column
format and this is how I was swapping directions previously:

 def swap_directions(list_):
    newlist = []
    row = []
    for x in xrange(len(list_[0])):
        for y in xrange(len(list_)):
            row.append(list_[y][x])
        newlist.append(row)
        row = []
    return newlist

I'm not suggesting you do it like this, but it's always useful to
> learn new patterns.
>

That's certainly true!

Thanks for the help, Rich and Lie. I think I can get it now.

-Wayne

 --
> Rich "Roadie Rich" Lovely
>
> There are 10 types of people in the world: those who know binary,
> those who do not, and those who are off by one.
>



-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn’t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090913/1e57ba1e/attachment.htm>


More information about the Tutor mailing list