Re: [Tutor] Sorting 2-d data
2009/9/13 Lie Ryan : > Wayne wrote: >> >> Hi, >> >> I have a set of data that looks something like this: >> >> 3, 4, 3, 2, 1 >> 2, 1, 1, 1, 1 >> 4, 2, 2, 1, 2 >> 1, 3, 1, 1, 1 >> >> I want to be able to sort it by the first column, keeping the rest of the >> values in the same position relative to the original: >> >> 1, 3, 1, 1, 1 >> 2, 1, 1, 1, 1 >> 3, 4, 3, 2, 1 >> 4, 2, 2, 1, 2 >> >> and I'm wondering if there are any included ways to sort data like this. >> I've currently got a 2d list by columns, but I could easily convert that to >> rows if it would work better. >> > > if you have data like this: > mylist = [ >[3, 4, 3, 2, 1], >[2, 1, 1, 1, 1], >[4, 2, 2, 1, 2], >[1, 3, 1, 1, 1], > ] > > you can use mylist.sort(key=lambda x: x[0]) > > sorted(mylist, key=lambda x: x[0]) works as well if you need a new list > > 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. > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > This is another way, but will probably be slower than re-zipping into row-major order. keys = mylist[0] myNewList = [] for L in mylist: decoratedList = zip(keys, L) decoratedList.sort(key=lambda x: x[0]) undecoratedList = map(lambda x: x[1], decoratedList) myNewList.append(undecoratedList) This is known as the decorate-sort-undecorate pattern. It is possible to turn it into a one line comprehension: myNewList = [[decorated[1] for decorated in sorted(zip(mylist[0], L), key=lambda t: t[0])] for L in mylist] I'm not suggesting you do it like this, but it's always useful to learn new patterns. -- 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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting 2-d data
On Sun, Sep 13, 2009 at 7:27 AM, Rich Lovely wrote: > 2009/9/13 Lie Ryan : > > Wayne wrote: > >> > > 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Still Trying to Understand GAE
Hi All, i've started earning python sone months ago (on Google App Engine unfortunately). I have some doubts reagrding "import", and have asked a similar question here months ago, but without finding a solution. So: with import i can import modules or single functions. And this is ok. Then: as i have understood from all the books i readm in each package directory i have the __init__.py file that decides what import with it. In other words if my package skel is like: /gg/ /gg/sub1/ /gg/sub1/file.py /gg/sub2/ /gg/sub2/file.py and i use "import gg", nothing is imported. To import sub1 and sub2, i can: - Put in /gg/ a __init__.py file that tells to import them - Use "from gg import sub1" Ok now the $1 Billion question: google app engine has the same schema than my "gg" package, an empty __init__.py file, but if i use "import google" it also imports all subdirectories. And i can't understand wiìhy it does so. Can you help me? Thankyou Giorgio ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting 2-d data
On Sun, Sep 13, 2009 at 1:17 AM, Lie Ryan wrote: > if you have data like this: > mylist = [ > [3, 4, 3, 2, 1], > [2, 1, 1, 1, 1], > [4, 2, 2, 1, 2], > [1, 3, 1, 1, 1], > ] > > you can use mylist.sort(key=lambda x: x[0]) You can omit the key parameter completely in this case, unless you want to preserve the order of lines whose first element are the same. The default comparison of lists is lexicographic, meaning it will compare the first element, if they are the same then compare the second, etc. > 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. zip() is the best method I know to do this. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting 2-d data
Lie Ryan wrote: if you have data like this: mylist = [ [3, 4, 3, 2, 1], [2, 1, 1, 1, 1], [4, 2, 2, 1, 2], [1, 3, 1, 1, 1], ] you can use mylist.sort(key=lambda x: x[0]) sorted(mylist, key=lambda x: x[0]) works as well if you need a new list I have a further question. If I want to sort the list not based on first element, but the second, third and so on, then using the lambda works nicely. But for sorting the list with the first element as key, I tried it using just mylist.sort() without the lambda, and its working also. Then why use the lambda? Sorry, I'm using python3. so maybe it won't work on python2 with just mylist.sort()? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Still Trying to Understand GAE
hi Giorgio, welcome to Python (whether directly or from GAE!) :-) my comments below. > with import i can import modules or single functions. And this is ok. not quite true. regardless of whether you use import or from-import, you're *always* importing (and loading) modules or packages in their entirety. now, whether you have *access* to "entire" modules/packages or individual attributes (functions, classes, or standard data), is another matter -- usually this is a result of using from-import. also, the difference between importing and loading is that loading only happens the first time you import a module/package. (if you do it more than once, e.g., module A imports B and C and module B also imports C, the import of C happens twice but the loading happens only once. > if i use "import > google" it also imports all subdirectories. And i can't understand > wiìhy it does so. it imports all sub*packages*. don't think directories because the import mechanism doesn't work this way. this is likely because there are __init__.py files in those subdirectories. another possibility is that there are non-empty __init__.py files that do the imports of things that you're not expecting. hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting 2-d data
> But for sorting the list with the first element as key, I tried it using just mylist.sort() without the lambda, and its working also. Then why use the lambda? There is a little difference between those two variations. Example: >>> sorted([[1,2],[1,3],[1,1]]) [[1, 1], [1, 2], [1, 3]] >>> sorted([[1,2],[1,3],[1,1]], key=lambda x:x[0]) [[1, 2], [1, 3], [1, 1]] For sorting it is necessary to compare items. So for example [1,1] is compared to [1,2] As you see: >>> [1,1] < [1,2] True If you apply the lambda things change: >>> (lambda x: x[0])([1,1])<(lambda x:x[0])([1,2]) False >>> (lambda x: x[0])([1,1])==(lambda x:x[0])([1,2]) True This is because now only the first item of the list is compared. - Patrick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] include remote module
On Saturday 12 September 2009, Patrick wrote: > Imagine if I gave a client a live Linux CD that automatically > called this sort of script right after boot up. They could have the > absolute most current version of an application from my server > launched on their box and if their data from that application was For this kind of application you could tansfer *.tar.gz or *.rmp files and unpack/install them. This would also be more general. Kind regards, Eike. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
I want to take two tuples and and print out all the elements in both tuples Best Weight Loss Program - Click Here! http://thirdpartyoffers.juno.com/TGL2141/fc/BLSrjpTFoYayK1TMy2HLJuXWZVUVwGcRz3BvRi27bhNCVhUuOvezC0KPl88/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
Go for it! 8^D On Sun, Sep 13, 2009 at 7:45 PM, shellc...@juno.com wrote: > I want to take two tuples and and print out all the elements in both tuples > > > Best Weight Loss Program - Click Here! > http://thirdpartyoffers.juno.com/TGL2141/fc/BLSrjpTFoYayK1TMy2HLJuXWZVUVwGcRz3BvRi27bhNCVhUuOvezC0KPl88/ > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- b h a a l u u at g m a i l dot c o m Gnu/Linux IS user-friendly. It's NOT ignorant-friendly or idiot-friendly. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
Please provide a meaningful subject (other than no subject) shellc...@juno.com wrote: I want to take two tuples and and print out all the elements in both tuples Please explain your goals more thoroughly. A trivial answer to your question is: tuple1 = (2.3) tuple2 = (3,4) print tuple1, tuple2 You probably want something different. So explain please. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor