Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / sequencematcher-examples.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="differ-objects.html" />
13<link rel="prev" href="sequence-matcher.html" />
14<link rel="parent" href="module-difflib.html" />
15<link rel="next" href="differ-objects.html" />
16<meta name='aesop' content='information' />
17<title>4.4.2 SequenceMatcher Examples </title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="4.4.1 SequenceMatcher Objects"
25 href="sequence-matcher.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="4.4 difflib "
28 href="module-difflib.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="4.4.3 Differ Objects"
31 href="differ-objects.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="sequence-matcher.html">4.4.1 SequenceMatcher Objects</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="module-difflib.html">4.4 difflib </A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="differ-objects.html">4.4.3 Differ Objects</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H2><A NAME="SECTION006420000000000000000"></A><A NAME="sequencematcher-examples"></A>
56<BR>
574.4.2 SequenceMatcher Examples
58</H2>
59
60<P>
61This example compares two strings, considering blanks to be ``junk:''
62
63<P>
64<div class="verbatim"><pre>
65&gt;&gt;&gt; s = SequenceMatcher(lambda x: x == " ",
66... "private Thread currentThread;",
67... "private volatile Thread currentThread;")
68</pre></div>
69
70<P>
71<tt class="method">ratio()</tt> returns a float in [0, 1], measuring the similarity
72of the sequences. As a rule of thumb, a <tt class="method">ratio()</tt> value over
730.6 means the sequences are close matches:
74
75<P>
76<div class="verbatim"><pre>
77&gt;&gt;&gt; print round(s.ratio(), 3)
780.866
79</pre></div>
80
81<P>
82If you're only interested in where the sequences match,
83<tt class="method">get_matching_blocks()</tt> is handy:
84
85<P>
86<div class="verbatim"><pre>
87&gt;&gt;&gt; for block in s.get_matching_blocks():
88... print "a[%d] and b[%d] match for %d elements" % block
89a[0] and b[0] match for 8 elements
90a[8] and b[17] match for 6 elements
91a[14] and b[23] match for 15 elements
92a[29] and b[38] match for 0 elements
93</pre></div>
94
95<P>
96Note that the last tuple returned by <tt class="method">get_matching_blocks()</tt> is
97always a dummy, <code>(len(<var>a</var>), len(<var>b</var>), 0)</code>, and this is
98the only case in which the last tuple element (number of elements
99matched) is <code>0</code>.
100
101<P>
102If you want to know how to change the first sequence into the second,
103use <tt class="method">get_opcodes()</tt>:
104
105<P>
106<div class="verbatim"><pre>
107&gt;&gt;&gt; for opcode in s.get_opcodes():
108... print "%6s a[%d:%d] b[%d:%d]" % opcode
109 equal a[0:8] b[0:8]
110insert a[8:8] b[8:17]
111 equal a[8:14] b[17:23]
112 equal a[14:29] b[23:38]
113</pre></div>
114
115<P>
116See also the function <tt class="function">get_close_matches()</tt> in this module,
117which shows how simple code building on <tt class="class">SequenceMatcher</tt> can be
118used to do useful work.
119
120<P>
121
122<DIV CLASS="navigation">
123<div class='online-navigation'>
124<p></p><hr />
125<table align="center" width="100%" cellpadding="0" cellspacing="2">
126<tr>
127<td class='online-navigation'><a rel="prev" title="4.4.1 SequenceMatcher Objects"
128 href="sequence-matcher.html"><img src='../icons/previous.png'
129 border='0' height='32' alt='Previous Page' width='32' /></A></td>
130<td class='online-navigation'><a rel="parent" title="4.4 difflib "
131 href="module-difflib.html"><img src='../icons/up.png'
132 border='0' height='32' alt='Up One Level' width='32' /></A></td>
133<td class='online-navigation'><a rel="next" title="4.4.3 Differ Objects"
134 href="differ-objects.html"><img src='../icons/next.png'
135 border='0' height='32' alt='Next Page' width='32' /></A></td>
136<td align="center" width="100%">Python Library Reference</td>
137<td class='online-navigation'><a rel="contents" title="Table of Contents"
138 href="contents.html"><img src='../icons/contents.png'
139 border='0' height='32' alt='Contents' width='32' /></A></td>
140<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
141 border='0' height='32' alt='Module Index' width='32' /></a></td>
142<td class='online-navigation'><a rel="index" title="Index"
143 href="genindex.html"><img src='../icons/index.png'
144 border='0' height='32' alt='Index' width='32' /></A></td>
145</tr></table>
146<div class='online-navigation'>
147<b class="navlabel">Previous:</b>
148<a class="sectref" rel="prev" href="sequence-matcher.html">4.4.1 SequenceMatcher Objects</A>
149<b class="navlabel">Up:</b>
150<a class="sectref" rel="parent" href="module-difflib.html">4.4 difflib </A>
151<b class="navlabel">Next:</b>
152<a class="sectref" rel="next" href="differ-objects.html">4.4.3 Differ Objects</A>
153</div>
154</div>
155<hr />
156<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
157</DIV>
158<!--End of Navigation Panel-->
159<ADDRESS>
160See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
161</ADDRESS>
162</BODY>
163</HTML>