Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex


4.4.2 SequenceMatcher Examples

This example compares two strings, considering blanks to be ``junk:''

>>> s = SequenceMatcher(lambda x: x == " ",...                     "private Thread currentThread;",...                     "private volatile Thread currentThread;")

ratio() returns a float in [0, 1], measuring the similarityof the sequences. As a rule of thumb, aratio() value over0.6 means the sequences are close matches:

>>> print round(s.ratio(), 3)0.866

If you're only interested in where the sequences match,get_matching_blocks() is handy:

>>> for block in s.get_matching_blocks():...     print "a[%d] and b[%d] match for %d elements" % blocka[0] and b[0] match for 8 elementsa[8] and b[17] match for 6 elementsa[14] and b[23] match for 15 elementsa[29] and b[38] match for 0 elements

Note that the last tuple returned byget_matching_blocks() isalways a dummy,(len(a), len(b), 0), and this isthe only case in which the last tuple element (number of elementsmatched) is0.

If you want to know how to change the first sequence into the second,useget_opcodes():

>>> for opcode in s.get_opcodes():...     print "%6s a[%d:%d] b[%d:%d]" % opcode equal a[0:8] b[0:8]insert a[8:8] b[8:17] equal a[8:14] b[17:23] equal a[14:29] b[23:38]

See also the functionget_close_matches() in this module,which shows how simple code building onSequenceMatcher can beused to do useful work.


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp