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 =
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
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
> 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'
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
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():
> Â
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
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
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":
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
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
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
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
13 matches
Mail list logo