Home > Enterprise >  Comparing the Nth letter to Nth letters of multiple strings in python
Comparing the Nth letter to Nth letters of multiple strings in python

Time:10-04

I can't quite figure this one out.

I have multiple five letter long strings and I want to compare each of the letters of the strings to a single string, and then to know if any of the Nth letters of the strings are equal to the Nth letter of the string I'm comparing them to, like this:

string_1 = 'ghost'
string_2 = 'media'
string_3 = 'blind'

the_word = 'shine'

if the_word[0] == string_1[0] or the_word[0] == string_2[0] or the_word[0] == string_3[0] or the_word[1] == string_1[1] or the_word[1] == string_2[1]... and so on...
  print('The Nth letter of some of the strings is equal to the Nth letter of the_word')
else:
  print('None of the letters positions correspond')

If there are multiple strings I want to compare the if statement gets very long so there must be a better way of doing this.

I would also like to know what the corresponding letters are (in this case they would be H (string_1[1] == the_word[1]), I (string_3[2] == the_word[2]) and N (string_3[3] == the_word[3])

If there are more than one corresponding letters I would like the return to be list containing all of the letters.

Also I dont need to know if the corresponding letter was the first or whatever the letters position in the word is, only if there are any (and what) corresponding letters.

I find this kind of hard to explain so sorry for possible confusion, will be happy to elaborate.

Thank you!

CodePudding user response:

IIUC, you can get to what you want using zip -

base_strings = zip(string_1, string_2, string_3)
for cmp_pair in zip(the_word, base_strings):
    if (cmp_pair[0] in cmp_pair[1]):
        print(cmp_pair[0])

Output

h
i
n

CodePudding user response:

You can extract the logic to a dedicated function and call it over each character of the string to be checked:

string_1 = 'ghost'
string_2 = 'media'
string_3 = 'blind'

the_word = 'shine'

def check_letter(l, i, words):
    match = []
    for w in words:
        if w[i] == l:
            match.append(w)
    return match

for i in range(len(the_word)):
    l = the_word[i]
    print("checking letter: {}".format(l))
    match = check_letter(l, i, [string_1, string_2, string_3])
    if (len(match) > 0):
        print("found in: {}".format(match))
    else:
        print("found in: -")

The above code results in:

$ python3 test.py
checking letter: s
found in: -
checking letter: h
found in: ['ghost']
checking letter: i
found in: ['blind']
checking letter: n
found in: ['blind']
checking letter: e
found in: -

CodePudding user response:

Maybe this answers your question:

strings = ['ghost', 'media', 'blind']
the_word = 'shine'

for s in strings:
    check = []
    lett = []
    for i in range(len(s)):
        if s[i] == the_word[i]:
            check.append(i)
            lett.append(s[i])
    if check:
        print('The letters {0} (position {1}) of the string {2} match to 
        the word {3}'.format(lett,check,s,the_word))
    else:
        print('No match between {0} and {1}'.format(s,the_word))

CodePudding user response:

Well one straight forward way would be the following:

string_1 = 'ghost'
string_2 = 'media'
string_3 = 'blind'
string_4 = 'trenn'

the_word = 'shine'

string_list = [string_1, string_2, string_3]

duplicate_letters_list = []
for string in string_list:
    for i in range(5):
        if the_word[i] == string[i]:
            print(f'{i}th letter is in {string} is a duplicate')
            if the_word[i] not in duplicate_letters_list:
                duplicate_letters_list.append(the_word[i])
print(duplicate_letters_list)

Output

1th letter is in ghost is a duplicate
2th letter is in blind is a duplicate
3th letter is in blind is a duplicate

['h', 'i', 'n']
  • Related