Re: sorting tuples...
Magnus Lycka wrote: > Why? It seems you are trying to use a string as some kind of container, > and Python has those in the box. Just use a list of tuples, rather than > a list of strings. That will work fine for .sort(), and it's much more > convenient to access your data. Using the typical tool for extracting > binary data from files/strings will give you tuples by default. my problem with tuples & lists is that i don't know how to assign data to them properly. i'm quite new in python ;) with the binary stuff out of the way, what i have is this string data: 20050922 # date line mike mike's message... 20040825 # date line jeremy jeremy's message... ... what i want to do is to use the date line as the first data in a tuple and the succeeding lines goes into the tuple, like: (20050922, mike, mike's message) then when it matches another date line it makes another new tuple with that date line as the header data and the succeeding data, etc.. (20050922, mike, mike's message) (20040825, jeremy, jeremy's message) ... then i would sort the tuples according to the date. is there an easier/proper way of doing this without generating alot of tuples? thanks! for the help :) -- http://mail.python.org/mailman/listinfo/python-list
Re: sorting tuples...
Steve Holden wrote: > Dan Sommers wrote: > > On 27 Sep 2005 19:01:38 -0700, > > [EMAIL PROTECTED] wrote: > > > > > >>with the binary stuff out of the way, what i have is this string data: > > > > > >>20050922 # date line > >>mike > >>mike's message... > >>20040825 # date line > >>jeremy > >>jeremy's message... > >>... > > > > > >>what i want to do is to use the date line as the first data in a tuple > >>and the succeeding lines goes into the tuple, like: > > > > > >>(20050922, mike, mike's message) > > > > > >>then when it matches another date line it makes another new tuple with > >>that date line as the header data and the succeeding data, etc.. > > > > > >>(20050922, mike, mike's message) > >>(20040825, jeremy, jeremy's message) > >>... > > > > > >>then i would sort the tuples according to the date. > > > > > >>is there an easier/proper way of doing this without generating alot of > >>tuples? > > > > > > You want a dictionary. Python dictionaries map keys to values (in other > > languages, these data structures are known as hashes, maps, or > > associative arrays). The keys will be the dates; the values will depend > > on whether or not you have multiple messages for one date. > > > > If the dates are unique (which, looking at your data, is probably not > > true), then each item in the dictionary can be just one (who, message) > > tuple. > > > > If the dates are not unique, then you'll have to manage each item of the > > dictionary as a list of (who, message) tuples. > > > > And before you ask: no, dictionaries are *not* sorted; you'll have to > > sort a separate list of the keys or the items at the appropriate time. > > > I'm not sure this advice is entirely helpful, since it introduces > complexities not really required by the simplistic tuple notation the OP > seems to be struggling for. > > Following the old adage "First, make it work; then (if it doesn't work > fast enough) make it faster)", and making the *dangerous* assumption > that each message genuinely is exactly three lines, we might write: > > msglist = [] > f = open("theDataFile.txt", "r") > for date in f: >who = f.next() # pulls a line from the file >msg = f.next() # pulls a line from the file >msglist,append((date, who, msg)) > # now have list of messages as tuples > msglist.sort() > > After this, msglist should be date-sorted list of messages. Though who > knows what needs to happen to them next ... just to spit it all out to stdout in a nice formatted form so I can save it to a file. I'm still confused though, but I'm working on it. struct is nice. > > regards > Steve > -- > Steve Holden +44 150 684 7255 +1 800 494 3119 > Holden Web LLC www.holdenweb.com > PyCon TX 2006 www.pycon.org -- http://mail.python.org/mailman/listinfo/python-list
sorting tuples...
Hello guys, I made a script that extracts strings from a binary file. It works. My next problem is sorting those strings. Output is like: snip 200501221530 John *** long string here *** 200504151625 Clyde *** clyde's long string here *** 200503130935 Jeremy *** jeremy string here snip How can I go about sorting this list based on the date string that marks the start of each message? Should I be using lists, dictionaries or tuples? What should I look into? Is there a way to generate variables in a loop? Like: x=0 while (x<10): # assign variable-x = [...list...] x = x+1 Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: sorting tuples...
Thank you very much. I'll look into this immediately. I edited my code earlier and came up with stringing the groups (200501202010, sender, message_string) into one string delimited by '%%%'. I could then sort the messages with the date string at the beginning as the one being sorted with the big string in its "tail" being sorted too. 200501202010%%%sender%%%message_string 200502160821%%%sender%%%message_string ... After sorting this list of long strings, I could then split them up using the '%%%' delimiter and arrange them properly for output. It's crude but at least I achieve what I wanted done. But both posters gave good advices, if not a bit too advanced for me. I'll play with them and keep tweaking my code. Thanks so much! -- /nh -- http://mail.python.org/mailman/listinfo/python-list