Pattern Matching Given # of Characters and no String Input; use RegularExpressions?

Synonymous sm.synonymous at gmail.com
Sun Apr 17 21:12:19 EDT 2005


tiissa <tiissa at nonfree.fr> wrote in message news:<42623ba8$0$10322$636a15ce at news.free.fr>...
> 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

I will look at that, although if i have 300 images i dont want to type
all the comparisons (In [9]: cmp('ccc','cccap')) by hand, it would
just be easier to sort them then :).

I got it somewhat close to working in visual basic:

If Left$(Cells(iRow, 1).Value, Count) = Left$(Cells(iRow - 1,
1).Value, Count) Then

What it says is when comparing a list, it looks at the 'Count' left
number of characters in the cell and compares it to the row cell
above's 'Count' left number of characters and then does the task (i.e.
makes a directory, moves the files) if they are equal.

I will look for a Left$(str) function that looks at the first X
characters for python :)).

Thank you for your help!

Synonymous



More information about the Python-list mailing list