python - 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.
解决方案
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]])
推荐阅读
- scala - Scalatest 提供 NaN 的匹配器吗?
- linux - 查找行并在末尾附加文本不起作用
- dynamics-crm - 出现错误,没有为给定实体定义实体映射
- leaflet - 无法使用 Leaflet 和 Leaflet knn 插件选择标记
- objective-c - 使用 AFNetWorking 上传文件是否有文件大小限制?
- java - 为什么使用 Java 的流 API 收集器进行管道分组需要更多时间?
- mongodb - 如何查询每五个文档的字段平均值?
- php - 我的 GET 请求(使用 Guzzle)在关闭客户端服务器之前不会发送(编辑:可能会被另一个请求阻塞?)
- pytorch - 当示例数量未完全除以批量大小时,Pytorch DataLoader 失败
- linux - 如何在将文本转换为 CSV 时从文件中删除双引号?