If you mean creating a dictionary with values, you can do it like this:
dict_with_coding = { 'a' : 1, 'b' : 2, 'c' : 3 }
Etc. by the list. You can also generate a dictionary using the random module.
You can encode as follows (the method returns a new string, since the strings in Python immutable):
def code_string(input_str : str, coding_dict) -> str: #Получаем список символов list_with_symbols = list(input_str) for i in range(len(list_with_symbols)): #Этот фрагмент также можно реализовать с помощью метода dict.setdefault #чтобы исключить случаи отсутствия ключа в словаре list_with_symbols[i] = str(coding_dict[list_with_symbols[i]]) return ''.join(sym for sym in list_with_symbols)
Let's do some tests:
dict_with_coding = { 'a' : 1, 'b' : 2, 'c' : 3 } print(code_string('aabbccc', dict_with_coding)) print(code_string('babcac', dict_with_coding)) print(code_string('cabcbab', dict_with_coding))