Re: Merging ordered lists

2008-06-03 Thread etal
On Jun 3, 1:22 am, Peter Otten <[EMAIL PROTECTED]> wrote: > > Yes :) > > Seriously, you are using O(n) containers and O(n) lookup where mine uses > O(1). For short lists it doesn't matter, but as the list length grows the > difference gets huge: > > $ cat unique.py > def unique(items): >     u =

Re: Merging ordered lists

2008-06-03 Thread etal
On Jun 2, 11:08 pm, Raymond Hettinger <[EMAIL PROTECTED]> wrote: > > If the inputs were not sorted, then I don't think you have a precise > idea of what it means to merge them while preserving order.   For > example if the inputs are XYZPDQ and bYlmPz, then what does a merged > sequence look like

Re: Merging ordered lists

2008-06-03 Thread Peter Otten
etal wrote: > > def unique(items): > >     u = set(items) > >     if len(u) == len(items): > >         return items > >     result = [] > >     for item in items: > >         if item in u: > >             result.append(item) > >             u.remove(item) > >     retu

Re: Merging ordered lists

2008-06-02 Thread Raymond Hettinger
> Thanks for the tip; itertools never ceases to amaze. One issue: > groupby doesn't seem to remove all duplicates, just consecutive ones > (for lists of strings and integers, at least): > > >>> [k for k, g in itertools.groupby(list("asdfdfffdf"))] > > ['a', 's', 'd', 'f', 'd', 'f', 'd', 'f'] That'

Re: Merging ordered lists

2008-06-02 Thread etal
On Jun 1, 12:34 am, Raymond Hettinger <[EMAIL PROTECTED]> wrote: > > I would do it two steps.  There's a number of ways to merge depending > on whether everything is pulled into memory or > not:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/491285http://aspn.activestate.com/ASPN/Cookboo

Re: Merging ordered lists

2008-06-02 Thread etal
On Jun 1, 1:49 am, Peter Otten <[EMAIL PROTECTED]> wrote: > Peter Otten wrote: > > #untested > > Already found two major blunders :( > > # still untested > import difflib > > def _merge(a, b): >     sm = difflib.SequenceMatcher(None, a, b) >     for op, a1, a2, b1, b2 in sm.get_opcodes(): >  

Re: Merging ordered lists

2008-06-01 Thread Taekyon
etal wrote: > Speed actually isn't a problem yet; it might matter some day, but for > now it's just an issue of conceptual aesthetics. Any suggestions? Looks as if set does it for you. -- Taekyon -- http://mail.python.org/mailman/listinfo/python-list

Re: Merging ordered lists

2008-06-01 Thread MClaveau
Hi! Use set (union). Example: la=[2,1,3,5,4,6] lb=[2,8,6,4,12] #compact: print list(set(la).union(set(lb))) #detail: s1 = set(la) s2 = set(lb) s3 = s1.union(s2) print list(s3) @-salutations Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list

Re: Merging ordered lists

2008-06-01 Thread Peter Otten
Peter Otten wrote: > #untested Already found two major blunders :( # still untested import difflib def _merge(a, b): sm = difflib.SequenceMatcher(None, a, b) for op, a1, a2, b1, b2 in sm.get_opcodes(): if op == "insert": yield b[b1:b2] elif op == "replace":

Re: Merging ordered lists

2008-06-01 Thread M�ta-MCI (MVP)
Hi! Use set (union). Example: la=[2,1,3,5,4,6] lb=[2,8,6,4,12] #compact: print list(set(la).union(set(lb))) #detail: s1 = set(la) s2 = set(lb) s3 = s1.union(s2) print list(s3) @-salutations Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list

Re: Merging ordered lists

2008-06-01 Thread Peter Otten
etal wrote: > Here's an algorithm question: How should I efficiently merge a > collection of mostly similar lists, with different lengths and > arbitrary contents, while eliminating duplicates and preserving order > as much as possible? > > My code: > > def merge_to_unique(sources): > """Mer

Re: Merging ordered lists

2008-06-01 Thread Raymond Hettinger
On May 31, 10:00 pm, etal <[EMAIL PROTECTED]> wrote: > Here's an algorithm question: How should I efficiently merge a > collection of mostly similar lists, with different lengths and > arbitrary contents, while eliminating duplicates and preserving order > as much as possible? I would do it two s

Merging ordered lists

2008-05-31 Thread etal
Here's an algorithm question: How should I efficiently merge a collection of mostly similar lists, with different lengths and arbitrary contents, while eliminating duplicates and preserving order as much as possible? My code: def merge_to_unique(sources): """Merge the unique elements from eac