-1

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.

  • 1
    Welcome to Stack Overflow. It seems like you really have two questions: how to get each pair of strings for comparison, and how to compare two strings character-by-character. Each of these is a common question, for which I will attempt to link duplicates. – Karl Knechtel Oct 30 '21 at 18:41
  • Combine`itertools.combinations()` with `zip()` for each combination. – Green Cloak Guy Oct 30 '21 at 18:44
  • But please note that [you are expected to do some research yourself first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). Try starting by putting things like `python combinations` and `python compare elementwise` into a search engine; you can get more specific as you go. – Karl Knechtel Oct 30 '21 at 18:45
  • 1
    You already asked [the same question](https://stackoverflow.com/questions/69780944/comparison-between-string-characters-within-a-list) under a different username half an hour ago. – Thierry Lathuille Oct 30 '21 at 18:46

1 Answers1

-1
from itertools import combinations

input_list = ["dbc", "dei", "ghi"]
for compare_group in combinations(input_list, 2):
    print([ch for inx0, ch in enumerate(compare_group[0]) if ch == compare_group[1][inx0]])


PersianMan
  • 924
  • 1
  • 12
  • 29
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: https://stackoverflow.com/help/how-to-answer . Good luck – nima Oct 30 '21 at 20:43
  • @nima , yes , this is the first my answer that hasn't description, because when I knew question was duplicated i dropped it – PersianMan Oct 31 '21 at 06:44