Home > database >  Python: How to replace letters in string using lists?
Python: How to replace letters in string using lists?

Time:11-18

Basically, I'm creating a really low tier cipher. I've set up a bit of code to randomize each character, but I can't figure out how to replace a string with these. This is the code I attempted

characters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ".", ",", " ", "!", "?"]

characters2 = ['h', 'i', '.', 'u', 'o', 'x', 'q', 'b', 'y', 'z', 's', 'd', 'm', 'w', 'k', 'n', 'j', '?', 'a', 'v', 't', 'r', 'e', 'f', 'c', ' ', '!', 'l', 'g', 'p', ',']

string = string.replace(characters[],characters2[])

In this example I was basically expecting being able to input a string, such as "string" and get back the encrypted string, which in this case would come back as "av?ywq". The only other way I could think of working this out would basically be to write

string = string.replace(characters[0],characters2[0]).replace(characters[1],characters2[1]).replace...

for the entire length of the list, which I could do, but it would be extremely tedious and take up way too much space.

Doing a loop would of course mean that if, for example, the "i" in "string" were replaced with an "s", and then the "s" in string were replaced with an "h", it would come out "htrhng", replacing both the "i" and "s" with the "h".

How would I go about solving this?

CodePudding user response:

Welcome to SO. For these type of problems, it's probably smarter to build a new string and enter in the appropriate character based on its value in the original string. I would also highly consider using a dictionary that maps old_character --> new_character

character_mapping = {"a": "h", "b": "i" , ...}  

old_string = "my old string"

new_string = ""

for char in old_string:
    new_string  = character_mapping[char]

This way, you don't need to worry about indexes (from the lists). Also, please don't use string as a variable name as it's the name of a default package included in Python.

CodePudding user response:

You can use dictionary to map one list item to another .

string = "string"

characters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ".", ",", " ", "!", "?"]
characters2 = ['h', 'i', '.', 'u', 'o', 'x', 'q', 'b', 'y', 'z', 's', 'd', 'm', 'w', 'k', 'n', 'j', '?', 'a', 'v', 't', 'r', 'e', 'f', 'c', ' ', '!', 'l', 'g', 'p', ',']
    
dic =  {} 
for l1,l2 in zip(characters,characters2):
    dic[l1]=l2
    
result = ""
for letter in string:
    result = result   dic[letter]
print(result)

#Output = av?ywq

CodePudding user response:

translate() is an option.

import string

characters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ".", ",", " ", "!", "?"]
characters2 = ['h', 'i', '.', 'u', 'o', 'x', 'q', 'b', 'y', 'z', 's', 'd', 'm', 'w', 'k', 'n', 'j', '?', 'a', 'v', 't', 'r', 'e', 'f', 'c', ' ', '!', 'l', 'g', 'p', ',']

text = "string"
table = text.maketrans(dict(zip(characters, characters2)))
print(text.translate(table))

Output:

av?ywq
  • Related