[Tutor] Is there a prettier way to do this?

Douglas N. Shawhan lysdexia at crackrabbit.com
Fri Oct 1 05:53:39 CEST 2004


Kent Johnson wrote:

>
> # You don't need the string module, use the string methods instead
>
> search_list=search_str.split(",")

Yes! Very good.

>
> # search_list is now ['three', ' one']
> # Note the space before 'one' - this may not be what you want.
> # You could split on ', ' if you always have the space, or use a regex 
> split, or
> # omit the space in the original data...

Good advice!

>
> # Personally I like to get the key, value pairs if I am going to need 
> both
> # Is there a reason why you are using a dict? for this snippet a list 
> of pairs would
> # make more sense
>
> items = DB.items()
> items.sort()

Yes, this makes more sense and removes a step.

The problem is: I think I was unclear stating what the loop is supposed 
to do!

I need to find *more than one* string fragment per string.

Say I need to verify the existence of the sequences "joe", "bill" and 
"leon" in a string - but the next time I may need only find "joe" and 
"bill", which is why I went with the 0, 1 scheme.

It may be better stated as: "How do I find arbitrary numbers of 
substrings in arbitrary order in a string pythonically?"


More information about the Tutor mailing list