Re: How to implement function like this?
even shorter: def funcA(tarray): s = min(len(tarray), 3) return [2, 3, 4][0:s] + [e for e in funcB(3-s)[0:3-s]] -- http://mail.python.org/mailman/listinfo/python-list
Re: How to implement function like this?
Marc 'BlackJack' Rintsch a écrit : > On Tue, 23 Oct 2007 11:48:08 +0200, Loic Mahe wrote: > >> even shorter: >> >> def funcA(tarray): >> s = min(len(tarray), 3) >> return [2, 3, 4][0:s] + [e for e in funcB(3-s)[0:3-s]] > > Why the list comprehension!? > > Ciao, > Marc 'Blackjack' Rintsch sorry I just read too fast and thought he worked with lists ... anyway 'e for e in' and so list comprehension was useless here def funcA(tarray): s = min(len(tarray), 3) return (2, 3, 4,)[0:s] + funcB(3-s)[0:3-s] this is ok if funcB(...) returns a tuple ... if it returns a list just add: tuple(funcB(...)) note: list comprehension transforms a tuple into a list -- http://mail.python.org/mailman/listinfo/python-list
Re: Some "pythonic" suggestions for Python
Frank Samuelson a écrit : > foo = function(x,y) x+y*2 # Example S language code > bar = foo > bar(3,4) > m = lapply( s, foo ) > bb = lapply(s, function(t) t[3]*4 ) Here you want all functions to be lambda functions: you can get something very close to what you want, just like this: foo = lambda x,y: x+y*2 bar = foo bar(3,4) you would only need to extend existing lambda: * support all python keywords/expressions in lambda * multiline lambda * multi return value lambda * and possibility to map 'function' name to be equivalent to 'lambda' Loic -- http://mail.python.org/mailman/listinfo/python-list
Re: Some "pythonic" suggestions for Python
Chris M write : > Multi-return value lambda? Just so you know, there is no concept of > returning more than one value from a function. I wrote: > * multi return value lambda I meant: multiple return statement, not return multiple values pseudo code here: lambda x: if A return B, if C return D, if E return F ... it could be nice if ou could write normal functions ... as lambda ones and not being limited to a subset of the kewords ... at least it would fit Frank Samuleson needs/wishes better... -- http://mail.python.org/mailman/listinfo/python-list
Re: Equivalent of TCL's "subst" ?
gamename a écrit : > Hi, > > In TCL, you can do things like: > set foobar "HI!" > set x foo > set y bar > subst $$x$y > HI! > > Is there a way to do this type of evaluation in python? > > TIA, > -T > you can also try using eval: >>> foobar = "HI!" >>> x = 'foo' >>> y = 'bar' >>> print eval(x+y) HI! Loic -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert some Python code to C++
[EMAIL PROTECTED] a écrit : > For those that understand algorithms and can talk Python, I want to > convert the Python code in the section "Reading out all LCSs" into C++ > code but I don't understand some of the syntax. Can anyone give me a > hand? > > def backTrackAll(C, X, Y, i, j): > if i == 0 or j == 0: > return set([""]) > elif X[i-1] == Y[j-1]: > return set([Z + X[i-1] for Z in backTrackAll(C, X, Y, i-1, > j-1)]) > else: > R = set() > if C[i][j-1] >= C[i-1][j]: > R.update(backTrackAll(C, X, Y, i, j-1)) > if C[i-1][j] >= C[i][j-1]: > R.update(backTrackAll(C, X, Y, i-1, j)) > return R > > Thanks! > just have a look at this tutorial: and look for Lists and Sets http://docs.python.org/tut/tut.html and look in the reference index for set() and list() in http://docs.python.org/lib/genindex.html or just pick the algo in another language in the url you give or tell us precisely what part of the python algo you do not understant! -- http://mail.python.org/mailman/listinfo/python-list