Homap Homap - 3 months ago 6
Python Question

Check if two strings contain the same pattern in python

I have the following list:

names = ['s06_215','s06_235b','s06_235','s08_014','18:s08_014','s08_056','s08_169']


s06_235b
and
s06_235
,
s08_014
and
18:s08_014
are duplicated. However, as shown in the example, there is no specific pattern in the naming. I need to do a pairwise comparison of the element of the list:

for i in range(0, len(names)-1):
for index, value in enumerate(names):
print names[i], names[index]


I need then to check for each pair, if the two, contain the same string but with length more than
4
. That is
s06_235b
and
s06_235
, and
s08_014
and
18:s08_014
would pass this criterion but
s08_056
and
s08_169
would not.

How can I achieve this in Python?

Answer

You can use an 'in' operator to see if on variable contains another

if "example" in "this is an example":

Try this:

for i in range(0, len(names)-1):
    for index, value in enumerate(names):
       if names[i] in names[index] and len(names[i]) > 4:
          print names[i], names[index]
Comments