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

Mailing List Archive: Python: Python

algorithm does python use to compare two strings

 

 

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


jmwebaze at gmail

Apr 29, 2012, 12:59 AM

Post #1 of 4 (330 views)
Permalink
algorithm does python use to compare two strings

I am just wondering which specific algorithm does python use to compare two
strings. Could it be the Longest common subsequence is the most u

Regards

--
*Mob UG: +256 (0) 70 1735800 | NL +31 (0) 6 852 841 38 | Gtalk: jmwebaze |
skype: mwebazej | URL: www.astro.rug.nl/~jmwebaze

/* Life runs on code */*


tjreedy at udel

Apr 29, 2012, 3:05 AM

Post #2 of 4 (318 views)
Permalink
Re: algorithm does python use to compare two strings [In reply to]

On 4/29/2012 3:59 AM, J. Mwebaze wrote:
> I am just wondering which specific algorithm does python use to compare
> two strings.

'Python' does not use algorithms, implementations do. CPython may check
id and or hash before doing a character-by-char comparison (or perhaps
multiple chars at a time).

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Apr 29, 2012, 9:34 AM

Post #3 of 4 (315 views)
Permalink
Re: algorithm does python use to compare two strings [In reply to]

On 4/29/2012 6:05 AM, Terry Reedy wrote:
> On 4/29/2012 3:59 AM, J. Mwebaze wrote:
>> I am just wondering which specific algorithm does python use to compare
>> two strings.
>
> 'Python' does not use algorithms, implementations do. CPython may check
> id and or hash before doing a character-by-char comparison (or perhaps
> multiple chars at a time).

I think all the sequence comparisons check for equality of len() before
doing item by item comparison.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


robert.kern at gmail

Apr 29, 2012, 10:26 AM

Post #4 of 4 (312 views)
Permalink
Re: algorithm does python use to compare two strings [In reply to]

On 4/29/12 5:34 PM, Terry Reedy wrote:
> On 4/29/2012 6:05 AM, Terry Reedy wrote:
>> On 4/29/2012 3:59 AM, J. Mwebaze wrote:
>>> I am just wondering which specific algorithm does python use to compare
>>> two strings.
>>
>> 'Python' does not use algorithms, implementations do. CPython may check
>> id and or hash before doing a character-by-char comparison (or perhaps
>> multiple chars at a time).

Identity is checked first, but hashes aren't.

> I think all the sequence comparisons check for equality of len() before doing
> item by item comparison.

== does, but != and the ordering comparisons don't, at least in the 2.x str
implementation. unicode objects don't compare lengths first because the rich
comparison function really just defers to an old-style -1,0,+1 compare function.
In Python 3.x, bytes objects behave the same, but str objects *do* the length
comparison for both == and != (but not the others, naturally). <phew>

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

--
http://mail.python.org/mailman/listinfo/python-list

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


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.