yet another noob question

Jason Nordwick jason at adapt.com
Mon Aug 14 13:50:15 EDT 2006


Somehow my other response to the list got lost. I'm still learning Python, but this seems much better than my first attempt:

def pr(x): print x
def cross(x,y): return [a+b for a in x for b in y]
x=map(pr, reduce(cross, [map(str,range(1,6))]*5))

-j

Stargaming wrote:
> Jason Nordwick schrieb:
>> Or without filter:
>>
>> from operator import add
>> def pr(x): print x
>> def cross(x,y): return reduce(add, [[a+b for b in y] for a in x])
>> x=map(pr, reduce(cross, [map(str,range(1,6))]*5))
>>
> [...]
> 
> reduce(add, list) is the same as sum(list) and is only half as fast as sum:
>  >>> from timeit import Timer
>  >>> sum(Timer("sum(range(500))").repeat(200, 1000))/200
> 0.055693786500798058
>  >>> sum(Timer("reduce(add, range(500))", "from operator import 
> add").repeat(200, 1000))/200
> 0.10820861031220445
>  >>>
> 
> Also note that reduce will be removed in Python 3000.




More information about the Python-list mailing list