-4

Having a Python list, containing same length strings, like the following one:

input_list = [ "abc", "def", "ghi" ]

How can I compare character by character all the strings and do the difference between them? Each string has to compare the other once.

list[0] with list[1]

list[0] with list[2]

list[1] with list[2]

Example of a comparison:

"a" with "d"
"b" with "e"
"c" with "f" 

The number of string-type elements in this list may change, but the length of the strings will always be the same.

I have been struggling a lot trying to find a way to do this by either turning each element into a sub-list so I could compare them but the comparison would always give me errors in the loops such as "index out of range".

I also tried using dictionaries:

for i in range(len(list1)):
     n_dict["Player%s" %i] = list(list1[I])

This would give me the following output:

{'Player0': ['a', 'b', 'c'],
 'Player1': ['d', 'e', 'f'],
 'Player2': ['g', 'h', 'i']}

but then again, comparing was even more complicated with loops. I also tried by simply using list indexes such as

for i in range(len(list1):
    for j in range(len(list1):
        if ord(list[i][j]) - ord(list[i][j]) < k:
            player0 += 1
        else:
            player1 +=1

but always index out of range. Can someone please help me out?

  • You already asked the same question under a different username : https://stackoverflow.com/questions/69781124/how-to-compare-characters-of-strings-that-are-elements-of-a-list/69781225 – PersianMan Oct 30 '21 at 19:00
  • What exactly are you trying to obtain out of these comparisons ? If it is merely to see if the strings are equal, you can just compare the whole strings. If you are looking at computing a "distance" from character to character, then you should look into the zip() and abs() functions – Alain T. Oct 30 '21 at 21:36
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 31 '21 at 03:35

1 Answers1

0
l = ["abc","def","abc","def","abc"]


i1 = 0
i2 = 1
for i in l[:-1]:
    for j in l[i2:]:
        while len(i) > i1:
            print(i[i1]==j[i1])
            i1 += 1
        i1 = 0
    i2 += 1

You can compare strings character by character but don't get it what do you mean by do the difference ?

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 30 '21 at 19:40