regular expression problem

B

borges2003xx

hi everyone
there is a way, using re, to test (for es) in
a=[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14] if a list b is
composed by three "sublists" of a separated or not by elements.

if b=[a2,a3,a4,a7,a8,a12,a13] gives true because in a
we have [....,a2,a3,a3,...,a7,a8,...,a12,a13,...]
or b=[a1,a2,a5,a14] gives true because in a we have
[a1,a2,....,a5,...,a14] and so on...

thank in advance.
giorgio borghi
 
A

alex23

hi everyone
there is a way, using re, to test (for es) in
a=[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14] if a list b is
composed by three "sublists" of a separated or not by elements.

Heya,

Is there any particular reason why you need to use re?

If you're using Python 2.3 or greater, the sets module might be easier
to deal with here:
from sets import Set
a = Set([1,2,3,4,5,6,7,8,9,10,11,12,13,14])
b = Set([2,3,4,7,8,12,13])
b.issubset(a) True
b = Set([1,2,5,14])
b.issubset(a) True
b = Set([3,7,23,200])
b.issubset(a)
False

Sets are unsorted, I'm uncertain if that's a requirement for you.

Hope this helps.
-alex23
 
K

Kent Johnson

hi everyone
there is a way, using re, to test (for es) in
a=[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14] if a list b is
composed by three "sublists" of a separated or not by elements.

if b=[a2,a3,a4,a7,a8,a12,a13] gives true because in a
we have [....,a2,a3,a3,...,a7,a8,...,a12,a13,...]
or b=[a1,a2,a5,a14] gives true because in a we have
[a1,a2,....,a5,...,a14] and so on...

difflib.SequenceMatcher can do this for you:
>>> a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
>>> b = [2,3,4,7,8,12,13]
>>> import difflib
>>> sm = difflib.SequenceMatcher(None, a, b)
>>> sm.get_matching_blocks() [(1, 0, 3), (6, 3, 2), (11, 5, 2), (14, 7, 0)]
>>> b = [1, 2, 5, 14]
>>> sm = difflib.SequenceMatcher(None, a, b)
>>> sm.get_matching_blocks()
[(0, 0, 2), (4, 2, 1), (13, 3, 1), (14, 4, 0)]

You should test for len(sm.get_matching_blocks()) == 4 (the last element is a dummy)

Kent
 
B

borges2003xx

thank you again:
i used list and not set because order in my list is important.
in fact i'd like to apply this function to strings (or ordered
sequences of data).
For this reason proposed to use regular expression.
best regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top