有一个Python列表,包含相同长度的字符串,如下所示:
input_list = [ "abc", "def", "ghi" ]
如何逐个字符比较所有字符串并找出它们之间的差异?每个字符串必须比较另一个字符串一次。
list[0] with list[1]
list[0] with list[2]
list[1] with list[2]
比较示例:
"a" with "d"
"b" with "e"
"c" with "f"
该列表中字符串类型元素的个数可以改变,但字符串的长度始终保持不变。
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]])