Предполагая, что вам нужно будет сопоставлять только пары строк, одна из которых имеет ди git, а другая нет, вы можете l oop через все возможные пары кортежей, извлекать кортежи, когда они совпадают, затем объедините их строки. Я расширил ваш список примеров кортежей, чтобы показать, что код можно обобщать.
import itertools
list_of_tuples = [('0000************', 'b'), ('****1234********', 'a'),
('****1111****3333', 'b'), ('0000************', 'a')]
# combine two strings of the same length by digit
def combine_strings(str1, str2):
new_string = ''
for index, char in enumerate(str1):
if char.isdigit():
new_string += char
else:
new_string += str2[index]
return new_string
# return all pairs of tuples that have a matching second element
def get_pairs(list_of_tuples):
all_matches = []
for pair in itertools.combinations(list_of_tuples, 2):
if pair[0][1] == pair[1][1]:
all_matches.append(pair)
return all_matches
def combine_tuples(tuple1, tuple2):
return (combine_strings(tuple1[0], tuple2[0]), tuple1[1])
all_matched_tuples = get_pairs(list_of_tuples)
print(all_matched_tuples)
list_matched_tuples = []
for pair in all_matched_tuples:
list_matched_tuples.append(combine_tuples(pair[0], pair[1]))
Вывод:
> list_matched_tuples
[('00001111****3333', 'b'), ('00001234********', 'a')]