On Wed, Apr 16, 2008 at 10:01:57AM -0400, Gary Bernhardt wrote:
>
> Unique can be done by
> set(items)
> as long as the items are hashable. If you want a list back, just do
> list(set(items))
> This is O(n).
>
> Flatten can be done by
> sum(lists, [])
> but it's O(n**2). Then again, so is your first definition of flatten
> because you're popping from arbitrary list indexes inside a loop. I
> think your flatten_original might be O(n**2) as well, and I'm not even
> going to try to decipher flatten_continuation. ;)
The naive version is what, two lines? :)
import itertools
def flatten(input, rtype=None):
if not input:
if rtype==None:
return input
else:
return rtype()
if rtype == None:
rtype = type(input[0])
return rtype(itertools.chain(*input)) # note: actual work done here
--
David Terrell
[EMAIL PROTECTED]
((meatspace)) http://meat.net/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---