Re: Moving to functional programming

2008-07-16 Thread Terry Reedy
Ben Finney wrote: Paul Rubin http://[EMAIL PROTECTED] writes: I don't like square-bracket listcomps because they leak the index variables to the outside. According to PEP 289 URL:http://www.python.org/dev/peps/pep-0289, this is an acknowledged wart that will be fixed in Python 3.0. Has

Re: Moving to functional programming

2008-07-15 Thread Paul Rubin
James Fassett [EMAIL PROTECTED] writes: tuple_list = ( ('John', 'Doe'), ('Mark', 'Mason'), ('Jeff', 'Stevens'), ('Bat', 'Man') ) # the final functional way [result_list, _] = zip(*tuple_list) That's really ugly IMO. I'd use: result_list = list(x for (x,y) in

Re: Moving to functional programming

2008-07-15 Thread Ben Finney
Paul Rubin http://[EMAIL PROTECTED] writes: I don't like square-bracket listcomps because they leak the index variables to the outside. According to PEP 289 URL:http://www.python.org/dev/peps/pep-0289, this is an acknowledged wart that will be fixed in Python 3.0. -- \“None can love

Re: Moving to functional programming

2008-07-14 Thread James Fassett
On Jul 12, 12:18 am, George Sakkis [EMAIL PROTECTED] wrote: It relies on positional arguments, tuple unpacking and the signature of zip(), It moreso relies on the fact that: t1 = (0,1,2,3) t2 = (7,6,5,4) [t1, t2] == zip(*zip(t1, t2)) True This is mathematically true given the definition of

Re: Moving to functional programming

2008-07-14 Thread [EMAIL PROTECTED]
On 14 juil, 11:51, James Fassett [EMAIL PROTECTED] wrote: On Jul 12, 12:18 am, George Sakkis [EMAIL PROTECTED] wrote: It relies on positional arguments, tuple unpacking and the signature of zip(), It moreso relies on the fact that: t1 = (0,1,2,3) t2 = (7,6,5,4) [t1, t2] ==

Re: Moving to functional programming

2008-07-14 Thread Terry Reedy
James Fassett wrote: zip ... robust and efficient than the list comprehension. I don't know the internals of how the Python interpreter treats list comprehensions and zip but it seems reasonable to assume an extra list is created for the zip approach. Minor times differences between this

Moving to functional programming

2008-07-11 Thread James Fassett
Hi all, Had a simple problem that turned into an interesting solution and I thought I would share it here. I had a list of tuples that I needed to get the first value from and generate a list. tuple_list = ( ('John', 'Doe'), ('Mark', 'Mason'), ('Jeff', 'Stevens'), ('Bat', 'Man')

Re: Moving to functional programming

2008-07-11 Thread bearophileHUGS
James Fassett: # the first Pythonic attempt using comprehensions result_list = [x[0] for x in tuple_list] # the final functional way [result_list, _] = zip(*tuple_list) I really like how Python allows me to do what I feel is the most natural solution (for a seasoned procedural programmer)

Re: Moving to functional programming

2008-07-11 Thread craig75
On Jul 11, 3:36 am, [EMAIL PROTECTED] wrote: James Fassett: # the first Pythonic attempt using comprehensions result_list = [x[0] for x in tuple_list] # the final functional way [result_list, _] = zip(*tuple_list) I really like how Python allows me to do what I feel is the most

Re: Moving to functional programming

2008-07-11 Thread sturlamolden
On Jul 11, 12:00 pm, James Fassett [EMAIL PROTECTED] wrote: tuple_list = (     ('John', 'Doe'),     ('Mark', 'Mason'),     ('Jeff', 'Stevens'),     ('Bat', 'Man')   ) # what I'd do in C or other procedural languages result_list = [] for item in tuple_list:    

Re: Moving to functional programming

2008-07-11 Thread Terry Reedy
[EMAIL PROTECTED] wrote: James Fassett: # the first Pythonic attempt using comprehensions result_list = [x[0] for x in tuple_list] This has the virtue of working for tuples of any length and doing the minimal work required. # the final functional way [result_list, _] = zip(*tuple_list)

Re: Moving to functional programming

2008-07-11 Thread George Sakkis
On Jul 11, 1:00 pm, James Fassett [EMAIL PROTECTED] wrote: Hi all, Had a simple problem that turned into an interesting solution and I thought I would share it here. I had a list of tuples that I needed to get the first value from and generate a list. tuple_list = (     ('John', 'Doe'),