[Python-ideas] More power in list comprehensions with the 'as' keyword

Ron Adam rrr at ronadam.com
Thu Aug 28 19:41:20 CEST 2008



Josiah Carlson wrote:
> The split/strip stuff is simple on purpose; it's fast.  Tossing in
> regular expressions handling is a great way to slow down the general
> case, never mind if you actually want to split on the passed literal
> string.

That's easy to remedy without making it overly complex.

 >>    text.split_pattern(' *\n+ *')


Cheers,
   Ron



>  - Josiah
> 
> On Thu, Aug 28, 2008 at 9:14 AM, Ron Adam <rrr at ronadam.com> wrote:
>>
>> Josiah Carlson wrote:
>>> -1 on the feature, I use the compound expression as below, only have
>>> the internal item be a generator expression to reduce peak memory
>>> usage.  Also, the != condition is unnecessary.
>>>
>>> [t for t in (t.strip() for t in text.split('\n')) if t]
>>
>>
>> If split was a bit smarter...
>>
>>    text.split(pattern=(' *\n+ *'))
>>
>>
>> ;-)
>>
>>
>> It can be done with the re module, but I need to look that up each time I
>> use it since I don't use it enough to remember all it's subtleties.
>>
>> Ron
>>
> 



More information about the Python-ideas mailing list