Pattern Matching Given # of Characters and no String Input; use RegularExpressions?
tiissa
tiissa at nonfree.fr
Sun Apr 17 06:34:17 EDT 2005
tiissa wrote:
> If you know the number of characters to match can't you just compare
> slices?
If you don't, you can still do it by hand:
In [7]: def cmp(s1,s2):
....: diff_map=[chr(s1[i]!=s2[i]) for i in range(min(len(s1),
len(s2)))]
....: diff_index=''.join(diff_map).find(chr(True))
....: if -1==diff_index:
....: return min(len(s1), len(s2))
....: else:
....: return diff_index
....:
In [8]: cmp('cccat','cccap')
Out[8]: 4
In [9]: cmp('ccc','cccap')
Out[9]: 3
In [10]: cmp('cccat','dddfa')
Out[10]: 0
More information about the Python-list
mailing list