>> > data /. x_?(# < 0 &) -> 0 (this is perhaps not the killer example)
>>
>> What does that do?
>
> /. is the pattern replacement operator, _ is a placeholder pattern
> that matches anything, x_ gives this placeholder a name so you can use
> it later, ? filters the matches (in this case, everything matches)
> with the pure function # < 0&, which takes one argument and returns
> true if the argument is less than 0, and the -> 0 part says anything
> that made it through the filter gets set to 0.  So
>
> In[40]:= data = {-1, 2, 3};
> In[41]:= data /. x_?(# < 0 &) -> 0
> Out[41]= {0, 2, 3}
>
> In other words
> sage: data = [-1,2,3]
> sage: [(d < 0 and [0] or [d])[0] for d in data]
> [0,2,3]

What about

sage: data = [-1, 2, 3]
sage: [(0 if d < 0 else d) for d in data]
[0, 2, 3]

Which is way clearer to me.

Arnaud

> I haven't been using python long enough to think this one is nice, but
> Mark Pilgrim says it's cool: 
> http://diveintopython.org/power_of_introspection/and_or.html#d0e9975
>

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to