Re: How to write this as a list comprehension?

2014-01-20 Thread Rustom Mody
On Monday, January 20, 2014 4:32:40 PM UTC+5:30, Piet van Oostrum wrote: > "Rhodri James" writes: > > On Sat, 18 Jan 2014 16:00:45 -, Jussi Piitulainen wrote: > [...] > >> I would write that on three lines anyway, properly indented: > >> [ somefunc(mn,day,wd,name) > >> for (then, name)

Re: How to write this as a list comprehension?

2014-01-20 Thread Piet van Oostrum
"Rhodri James" writes: > On Sat, 18 Jan 2014 16:00:45 -, Jussi Piitulainen > wrote: [...] >> I would write that on three lines anyway, properly indented: >> >> [ somefunc(mn,day,wd,name) >> for (then, name) in mylist >> let (_,mn,dy,_,_,_,wd,_,_) = localtime(then) ] >> >> It coul

Re: How to write this as a list comprehension?

2014-01-19 Thread Rhodri James
On Sat, 18 Jan 2014 16:00:45 -, Jussi Piitulainen wrote: Rustom Mody writes: On Saturday, January 18, 2014 2:06:29 PM UTC+5:30, Peter Otten wrote: > What would a list-comp with `let` or `where` look like? Would it > win the beauty contest against the loop? For me this is neat [somefu

Re: How to write this as a list comprehension?

2014-01-19 Thread Piet van Oostrum
John Allsup writes: > Hi, > > I'd agree with the advice that it's not the best idea: readability sucks > here, but consider the following: > > > import time > > def somefunc(a,b,c,d): # dummy function > return "{} - {} - {} : {}".format(a,b,c,d) > l = [(time.time(),"name {}".format(n)) for n

Re: How to write this as a list comprehension?

2014-01-18 Thread John Allsup
Hi, I'd agree with the advice that it's not the best idea: readability sucks here, but consider the following: import time def somefunc(a,b,c,d): # dummy function return "{} - {} - {} : {}".format(a,b,c,d) l = [(time.time(),"name {}".format(n)) for n in range(100)] # dummy data # the li

Re: How to write this as a list comprehension?

2014-01-18 Thread Piet van Oostrum
Alain Ketterlin writes: > Piet van Oostrum writes: > [...] >> Python misses a 'where' or 'let'-like construction as in Haskell. > > "let x = v in e" really is (lambda x:e)(v) > You are right, but it is a lot less readable IMHO. -- Piet van Oostrum WWW: http://pietvanoostrum.com/ PGP key: [8DAE

Re: How to write this as a list comprehension?

2014-01-18 Thread Jussi Piitulainen
Rustom Mody writes: > On Saturday, January 18, 2014 2:06:29 PM UTC+5:30, Peter Otten wrote: > > > What would a list-comp with `let` or `where` look like? Would it > > win the beauty contest against the loop? > > For me this is neat > > [somefunc(mn,day,wd,name) for (then, name) in mylist let >

Re: How to write this as a list comprehension?

2014-01-18 Thread Rustom Mody
On Saturday, January 18, 2014 2:06:29 PM UTC+5:30, Peter Otten wrote: > Options I can think of: > You could do it in two steps... > time_name_pairs = ((localtime(then), name) for then, name in mylist) > labels = [somefunc(t.tm_mon, t.tm_mday, t.tm_wday, name) > for t, name in time_na

Re: How to write this as a list comprehension?

2014-01-18 Thread Alain Ketterlin
Piet van Oostrum writes: [...] > I could define a auxiliary function like: > > def auxfunc(then, name): > _, mn, dy, _, _, _, wd, _, _ = localtime(then) > return somefunc(mn, day, wd, name) > > and then use > [auxfunc(then, name) for then, name in mylist] [...] > labels = [somefunc(mn,

Re: How to write this as a list comprehension?

2014-01-18 Thread Matěj Cepl
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 2014-01-17, 23:19 GMT, you wrote: > But defining the auxfunc takes away the elegance of a list > comprehension. Au contraire! Remember, that brevity is the sister of talent. I would definitively vote for labels = [make_label(then, name) f

Re: How to write this as a list comprehension?

2014-01-18 Thread Piet van Oostrum
Rustom Mody writes: > On Saturday, January 18, 2014 4:49:55 AM UTC+5:30, Piet van Oostrum wrote: [...] > >> Python misses a 'where' or 'let'-like construction as in Haskell. > > +1 > Yes Ive often been bitten by the lack of a 'comprehension-let' If it used only in a comprehension as in my exampl

Re: How to write this as a list comprehension?

2014-01-18 Thread Peter Otten
Piet van Oostrum wrote: > Hi, > > I am looking for an elegant way to write the following code as a list > comprehension: > > labels = [] > for then, name in mylist: > _, mn, dy, _, _, _, wd, _, _ = localtime(then) > labels.append(somefunc(mn, day, wd, name)) > > So mylist is a list of t

Re: How to write this as a list comprehension?

2014-01-17 Thread Rustom Mody
On Saturday, January 18, 2014 4:49:55 AM UTC+5:30, Piet van Oostrum wrote: > Hi, > I am looking for an elegant way to write the following code as a list > comprehension: > labels = [] > for then, name in mylist: > _, mn, dy, _, _, _, wd, _, _ = localtime(then) > labels.append(somefunc(mn,

Re: How to write this as a list comprehension?

2014-01-17 Thread Dan Stromberg
On Fri, Jan 17, 2014 at 3:19 PM, Piet van Oostrum wrote: > Hi, > > I am looking for an elegant way to write the following code as a list > comprehension: > > labels = [] > for then, name in mylist: > _, mn, dy, _, _, _, wd, _, _ = localtime(then) > labels.append(somefunc(mn, day, wd, name)

How to write this as a list comprehension?

2014-01-17 Thread Piet van Oostrum
Hi, I am looking for an elegant way to write the following code as a list comprehension: labels = [] for then, name in mylist: _, mn, dy, _, _, _, wd, _, _ = localtime(then) labels.append(somefunc(mn, day, wd, name)) So mylist is a list of tuples, the first member of the tuple is a time