Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Marc 'BlackJack' Rintsch
On Thu, 26 Jul 2007 00:58:10 +, beginner wrote: > I need nested lists to represent nested records in a script. Since the > structure of the underlying data is nested, I think it is probably > reasonable to represent them as nested lists. For example, if I have > the below structure: > > Big R

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Jeff
> For example, if I have > the below structure: > > Big Record >Small Record Type A >Many Small Record Type B >Small Record Type C > > It is pretty natural to use lists, although after a while it is > difficult to figure out the meaning of the fields in the lists. If > only there were a

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Steven D'Aprano
On Wed, 25 Jul 2007 09:33:26 -0700, Paul Rubin wrote: > Things > are logically single values or they are logically lists of values Except for strings, and string-like objects. And files. And records/structs, and tuples. And lists. And sets. And bit strings. And tree-like structures. And, we

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Steven D'Aprano
On Wed, 25 Jul 2007 15:46:58 +, beginner wrote: > I know the * operator. However, a 'partial unpack' does not seem to > work. > > def g(): > return (1,2) > > def f(a,b,c): > return a+b+c > > f(*g(),10) will return an error. No it doesn't, it _raises_ an exception. This is a function t

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread beginner
> Not the most beautiful solution, but it works. > > Diez- Hide quoted text - > > - Show quoted text - Yeah it works! Thanks. -- http://mail.python.org/mailman/listinfo/python-list

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread beginner
> Well, there are several ways to solve this. You could either invoke f(*g > () + (10,)). Might be a bit nasty and unreadable, though. Or you could > just change your function f to accept them in reversed order (f(10, *g) > should work) or convert g() to return a dictionary like {'b': 1, 'c': 2} >

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread beginner
> Also, this has not been suggested: > > py> def g(): > ... return (1,2) > ... > py> def f(a,b,c): > ... return a+b+c > ... > py> f(c=10, *g()) > 13 > > James- Hide quoted text - > > - Show quoted text - Great idea. -- http://mail.python.org/mailman/listinfo/python-list

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread beginner
On Jul 25, 11:33 am, Paul Rubin wrote: > beginner <[EMAIL PROTECTED]> writes: > > I know the * operator. However, a 'partial unpack' does not seem to work. > > A few other posters have mentioned ways around this, but you might ask > yourself what coding situation makes yo

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Jeff
On Jul 25, 3:05 pm, "Eduardo \"EdCrypt\" O. Padoan" <[EMAIL PROTECTED]> wrote: > def flatten(listOfLists): > return list(chain(*listOfLists)) > > >Fromhttp://www.python.org/doc/2.4/lib/itertools-recipes.html > > -- > EduardoOPadoan (eopadoan->altavix::com) > Bookmarks:http://del.icio.us/edcrypt

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Jeff
Sorry about that. Hopefully, this should work ;) def flatten(obj): if type(obj) not in (list, tuple, str): raise TypeError("String, list, or tuple expected in flatten().") if len(obj) == 1: if type(obj[0]) in (tuple, list): return flatten(obj[0]) else:

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Eduardo \"EdCrypt\" O. Padoan
def flatten(listOfLists): return list(chain(*listOfLists)) >From http://www.python.org/doc/2.4/lib/itertools-recipes.html -- EduardoOPadoan (eopadoan->altavix::com) Bookmarks: http://del.icio.us/edcrypt -- http://mail.python.org/mailman/listinfo/python-list

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Neil Cerutti
On 2007-07-25, Neil Cerutti <[EMAIL PROTECTED]> wrote: > On 2007-07-25, Jeff <[EMAIL PROTECTED]> wrote: >> Here's a quick flatten() function: >> >> def flatten(obj): >> if type(obj) not in (list, tuple, str): >> raise TypeError("String, list, or tuple expected in >> flatten().") >>

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Neil Cerutti
On 2007-07-25, Jeff <[EMAIL PROTECTED]> wrote: > Here's a quick flatten() function: > > def flatten(obj): > if type(obj) not in (list, tuple, str): > raise TypeError("String, list, or tuple expected in > flatten().") > if len(obj) == 1: > if type(obj[0]) in (tuple, list): >

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Aneesh Goel
On Jul 25, 10:33 am, Jeff <[EMAIL PROTECTED]> wrote: > def flatten(obj): > if type(obj) not in (list, tuple, str): > raise TypeError("String, list, or tuple expected in > flatten().") > if len(obj) == 1: > if type(obj[0]) in (tuple, list): > return flatten(obj[0]

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Jeff
Here's a quick flatten() function: def flatten(obj): if type(obj) not in (list, tuple, str): raise TypeError("String, list, or tuple expected in flatten().") if len(obj) == 1: if type(obj[0]) in (tuple, list): return flatten(obj[0]) else: ret

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread George Sakkis
On Jul 25, 12:00 pm, [EMAIL PROTECTED] wrote: > On Jul 25, 10:46 am, beginner <[EMAIL PROTECTED]> wrote: > > > > > On Jul 25, 10:19 am, Stargaming <[EMAIL PROTECTED]> wrote: > > > > On Wed, 25 Jul 2007 14:50:18 +, beginner wrote: > > > > Hi, > > > > > I am wondering how do I 'flatten' a list or

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread [EMAIL PROTECTED]
On Jul 25, 8:46 am, beginner <[EMAIL PROTECTED]> wrote: > On Jul 25, 10:19 am, Stargaming <[EMAIL PROTECTED]> wrote: > > > > > On Wed, 25 Jul 2007 14:50:18 +, beginner wrote: > > > Hi, > > > > I am wondering how do I 'flatten' a list or a tuple? For example, I'd > > > like to transform[1, 2, (3

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Wildemar Wildenburger
[EMAIL PROTECTED] wrote: > On Jul 25, 9:50 am, beginner <[EMAIL PROTECTED]> wrote: > >> Another question is how do I pass a tuple or list of all the >> aurgements of a function to the function. For example, I have all the >> arguments of a function in a tuple a=(1,2,3). Then I want to pass each

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread James Stroud
beginner wrote: > On Jul 25, 10:19 am, Stargaming <[EMAIL PROTECTED]> wrote: >> On Wed, 25 Jul 2007 14:50:18 +, beginner wrote: >>> Hi, >>> I am wondering how do I 'flatten' a list or a tuple? For example, I'd >>> like to transform[1, 2, (3,4)] or [1,2,[3,4]] to [1,2,3,4]. >> A recursive funct

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Paul Rubin
beginner <[EMAIL PROTECTED]> writes: > I know the * operator. However, a 'partial unpack' does not seem to work. A few other posters have mentioned ways around this, but you might ask yourself what coding situation makes you want to do this stuff in the first place. I won't say there's never a re

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Stargaming
On Wed, 25 Jul 2007 15:46:58 +, beginner wrote: > On Jul 25, 10:19 am, Stargaming <[EMAIL PROTECTED]> wrote: >> On Wed, 25 Jul 2007 14:50:18 +, beginner wrote: [snip] >> >> > Another question is how do I pass a tuple or list of all the >> > aurgements of a function to the function. For exa

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Diez B. Roggisch
beginner wrote: > On Jul 25, 10:19 am, Stargaming <[EMAIL PROTECTED]> wrote: >> On Wed, 25 Jul 2007 14:50:18 +, beginner wrote: >> > Hi, >> >> > I am wondering how do I 'flatten' a list or a tuple? For example, I'd >> > like to transform[1, 2, (3,4)] or [1,2,[3,4]] to [1,2,3,4]. >> >> A recur

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread beginner
On Jul 25, 11:00 am, [EMAIL PROTECTED] wrote: > On Jul 25, 10:46 am, beginner <[EMAIL PROTECTED]> wrote: > > > > > > > On Jul 25, 10:19 am, Stargaming <[EMAIL PROTECTED]> wrote: > > > > On Wed, 25 Jul 2007 14:50:18 +, beginner wrote: > > > > Hi, > > > > > I am wondering how do I 'flatten' a lis

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread kyosohma
On Jul 25, 10:46 am, beginner <[EMAIL PROTECTED]> wrote: > On Jul 25, 10:19 am, Stargaming <[EMAIL PROTECTED]> wrote: > > > > > On Wed, 25 Jul 2007 14:50:18 +, beginner wrote: > > > Hi, > > > > I am wondering how do I 'flatten' a list or a tuple? For example, I'd > > > like to transform[1, 2, (

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread beginner
On Jul 25, 10:19 am, Stargaming <[EMAIL PROTECTED]> wrote: > On Wed, 25 Jul 2007 14:50:18 +, beginner wrote: > > Hi, > > > I am wondering how do I 'flatten' a list or a tuple? For example, I'd > > like to transform[1, 2, (3,4)] or [1,2,[3,4]] to [1,2,3,4]. > > A recursive function, always yiel

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread Stargaming
On Wed, 25 Jul 2007 14:50:18 +, beginner wrote: > Hi, > > I am wondering how do I 'flatten' a list or a tuple? For example, I'd > like to transform[1, 2, (3,4)] or [1,2,[3,4]] to [1,2,3,4]. A recursive function, always yielding the first element of the list, could do the job. See the ASPN

Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread kyosohma
On Jul 25, 9:50 am, beginner <[EMAIL PROTECTED]> wrote: > Hi, > > I am wondering how do I 'flatten' a list or a tuple? For example, I'd > like to transform[1, 2, (3,4)] or [1,2,[3,4]] to [1,2,3,4]. > > Another question is how do I pass a tuple or list of all the > aurgements of a function to the f