re.search over a list
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Mon Oct 20 11:25:57 EDT 2008
Pat a écrit :
> Bruno Desthuilliers wrote:
>> Pat a écrit :
>>> While I can use a for loop looking for a match on a list, I was
>>> wondering if there was a one-liner way.
>>>
>>> In particular, one of my RE's looks like this '^somestring$' so I
>>> can't just do this: re.search( '^somestring$', str( mylist ) )
>>>
>>> I'm not smart enough (total newbie) to code up a generator expression
>>> and I was wondering if I'm missing something obvious.
>>
>> words = ['foo', 'bar', 'somestring', 'baaz']
>> re.search(r"^somestring$", "\n".join(words), re.MULTILINE)
>>
>>
>>> I love succinct but clearly understandable code.
>>
>> separator.join(sequence_of_strings) is a very common python idiom, so
>> you can consider it as readable.
>>
>
> That's excellent. Exactly what I was looking for.
>
> Thank you VERY much!
Note that at least for this exact case, you don't need re at all:
>>> 'somestring' in words
True
But I guess you do have some less trivial use case !-)
More information about the Python-list
mailing list