Search code examples
pythonsimilarity

How do I find what is similar in multiple strings in python?


I want to get what is similar in a few strings. For example, I have 6 strings:

HELLO3456
helf04g
hell0r
h31l0

I want to get what is similar in these strings, for example in this case I would like it to tell me something like:

h is always at the start

That example is pretty simple and I can figure that out in my head but with something like:

61TvA2dNwxNxmWziZxKzR5aO9tFD00Nj
pHHlgpFt8Ka3Stb5UlTxcaEwciOeF2QM
fW9K4luEx65RscfUiPDakiqp15jiK5f6
17xz7MYEBoXLPoi8RdqbgkPwTV2T2H0y
Jvt0B5uZIDPJ5pbCqMo12CqD7pdnMSEd
n7voYT0TVVzZGVSLaQNRnnkkWgVqxA3b

it's not that easy. I have seen and tried:

to name a few but they're all not what I'm looking for. They give a value of how similar they are and I need to know what is similar in them.

I want to know if this is even possible and if so, how I can do it. Thank you in advance.


Solution

  • I think this should be your desire solution. I have added "a" at the start of every string because otherwise there is no similarity in the strings you mentioned.

    lst = ["A61TvA2dNwxNxmWziZxKzR5aO9tFD00Nj","apHHlgpFt8Ka3Stb5UlTxcaEwciOeF2QM","afW9K4luEx65RscfUiPDakiqp15jiK5f6","a17xz7MYEBoXLPoi8RdqbgkPwTV2T2H0y", "aJvt0B5uZIDPJ5pbCqMo12CqD7pdnMSEd","an7voYT0TVVzZGVSLaQNRnnkkWgVqxA3b"]
    total_strings = len(lst)
    string_length = len(lst[0])
    for i in range(total_strings):
        lst[i] = lst[i].lower()
    
    for i in range(string_length):
        flag = 0
        lst_char = lst[total_strings-1][i]
        for j in range(total_strings-1):
            if lst[j][i] == lst_char:
                flag = 1
                continue
            else:
                flag = 0
                break
        if flag == 1:
            print(lst[total_strings-1][i]+" is always at position "+str(i))