Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

how to compare two strings?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


phr-n2002a at nightsong

Feb 12, 2002, 1:13 AM

Post #1 of 6 (553 views)
Permalink
how to compare two strings?

Is there any built-in function in Python for comparing two strings
alphabetically? Something suitable for passing as the optional
comparison function for the list "sort" method. I could code up
something like

def scmp(a,b):
if a<b: return -1
if b<a: return 1
return 0

but it's pretty ugly to need to do that. I've looked in the docs but
don't see any obvious counterpart to perl's lozenge (<=>) operator.
Is there one?

Thanks.


brian at sweetapp

Feb 12, 2002, 1:23 AM

Post #2 of 6 (511 views)
Permalink
how to compare two strings? [In reply to]

>>> print cmp.__doc__
cmp(x, y) -> integer

Return negative if x<y, zero if x==y, positive if x>y.
>>>

> Is there any built-in function in Python for comparing two strings
> alphabetically? Something suitable for passing as the optional
> comparison function for the list "sort" method. I could code up
> something like
>
> def scmp(a,b):
> if a<b: return -1
> if b<a: return 1
> return 0
>
> but it's pretty ugly to need to do that. I've looked in the docs but
> don't see any obvious counterpart to perl's lozenge (<=>) operator.
> Is there one?
>
> Thanks.
> --
> http://mail.python.org/mailman/listinfo/python-list


martin at v

Feb 12, 2002, 2:20 AM

Post #3 of 6 (515 views)
Permalink
how to compare two strings? [In reply to]

Paul Rubin <phr-n2002a[at]nightsong.com> writes:

> Is there any built-in function in Python for comparing two strings
> alphabetically?

Can you specify "alphabetically" more precisely? Do you mean
"lexicographically, by character ordinal value" (which is what your
scmp code suggests)? If so, the builtin cmp() function will do;
it compares strings just fine.

Or do you mean "according to the locale's conventions"? That would be
locale.strcoll. Notice that for sorting according to the locale's
conventions, it is more efficient to invoke strxfrm for each string,
and then sort by the transformed results.

Regards,
Martin


phr-n2002a at nightsong

Feb 12, 2002, 2:38 AM

Post #4 of 6 (521 views)
Permalink
how to compare two strings? [In reply to]

martin[at]v.loewis.de (Martin v. Loewis) writes:
> Can you specify "alphabetically" more precisely? Do you mean
> "lexicographically, by character ordinal value" (which is what your
> scmp code suggests)? If so, the builtin cmp() function will do;
> it compares strings just fine.

I meant exactly the same function that the sort method uses by default.
Yes it looks like cmp was what I wanted--I just didn't find it in the
obvious places in the docs (under the sort method or under string
services or string methods). Thanks.


scarblac at pino

Feb 12, 2002, 6:47 AM

Post #5 of 6 (532 views)
Permalink
how to compare two strings? [In reply to]

Paul Rubin <phr-n2002a[at]nightsong.com> wrote in comp.lang.python:
> Is there any built-in function in Python for comparing two strings
> alphabetically? Something suitable for passing as the optional
> comparison function for the list "sort" method. I could code up
> something like
>
> def scmp(a,b):
> if a<b: return -1
> if b<a: return 1
> return 0
>
> but it's pretty ugly to need to do that. I've looked in the docs but
> don't see any obvious counterpart to perl's lozenge (<=>) operator.
> Is there one?

Uhm. scmp() is the same as the builtin cmp(), as far as I can see.

Which means that this is the default action of sort(), no need to give it
any function.

--
Remco Gerlich


martin at v

Feb 12, 2002, 5:57 PM

Post #6 of 6 (514 views)
Permalink
how to compare two strings? [In reply to]

scarblac[at]pino.selwerd.nl (Remco Gerlich) writes:

> > def scmp(a,b):
> > if a<b: return -1
> > if b<a: return 1
> > return 0
[...]
> Uhm. scmp() is the same as the builtin cmp(), as far as I can see.

Almost, yes. It performs two comparisons, though, whereas the builtin
performs only one (in general).

Regards,
Martin

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.