首页 > 解决方案 > How to compare characters of strings that are elements of a list?

问题描述

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.

标签: pythonstringlist

解决方案


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]])



推荐阅读