[FRPythoneers] Joining a lists of lists?

Mike Olson Mike.Olson at fourthought.com
Tue Aug 6 16:15:59 MDT 2002


On Tue, 2002-08-06 at 16:06, marciot wrote:
> >If so then something like
> >
> >def joinlists(lists):
> >  res = {}
> >  for l in lists:
> >     for v in l:
> >        res[v] = 1
> >  k = res.keys()
> >  k.sort() #optional
> >  return k
> 
> You mean I actually have to write *code* to do this? Man... :(

Sure, your trying to implement a data type, set, that is not defined by
Python.  Been a long time since I used LISP, but when I played with it
it didn't have sets either so you would have to write code to do proper
combination of them in LISP as well.

The closest thing that Python has to sets is dictionaries, so, if you
are doing set manipulations, you would keep your data in a dictionary
and then it becomes as easy to join them.

t = [{1:1,2:1,3:1},{4:1,5:1,6:1},{7:1,8:1,9:1}]
reduce(lambda x,y: y.update(x),t,{})

Mike

> 
> >If not, then something like:
> >
> >>> t = [[1,2,3],[4,5,6],[7,8,9]]
> >>> reduce(lambda x,y:x+y,t,[])
> 
> Actually, this looks like what I'm needing. In fact, I was getting close, 
> since I had tried "reduce(append,t,[])" but Python complained about the 
> 'append'. I didn't realize you could use "+" on lists :)
> 
> Thanks for your help :)
> 
> Marcio Luis Teixeira
> 
> 
> PS: If anyone cares, I found a document that seems like might be a good 
> tutorial for the LISP inclined: http://www.norvig.com/python-lisp.html . Looks 
> like it might make for good reading.
> 
> 
> _______________________________________________
> This message sent by the FRPythoneers mailing list.
> Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
> URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers
-- 
Mike Olson                                Principal Consultant
mike.olson at fourthought.com                +1 303 583 9900 x 102
Fourthought, Inc.                         http://Fourthought.com 
4735 East Walnut St,                      http://4Suite.org
Boulder, CO 80301-2537, USA
XML strategy, XML tools, knowledge management




More information about the FRPythoneers mailing list