Austin Smith Austin Smith - 1 year ago 69
Python Question

Change character based off of its horizontal position? Python 2.7

"I am bad at explaining things, so if you think you know what I'm talking about and can explain it better: Feel free to suggest a full edit."

Lets say I have a string. I will not know the length of the string, It can be 2 characters, or 2,000, and contain a-z A-Z 0-9.

How can I make it so that I can change each character based off of their position from Left to Right?

What i need is to be able to predictably change matching characters in a string so that they don't match.

I need to be able to set some sort of seed to customize the result. Or, be able to define what characters turn into what.


I have the string "tttttttt"

I need it so that for each character, the outcome is different after this has run.

So the outcome could be something like "adktivep" or something.

This will be used to help me write an encryption concept to code. And before you ask, i have read why I shouldn't roll me own encryption.

I tested @bobsterman's code and decided i should explain this differently:

(Using his answer as a reference)

Okay, after a farther test i have concluded that this is not what i am looking for. I replaced
input_string = "HelloWorld"
input_string = "ttttttttttt"
and in console i got the result "vvvvvvvvvvv". You see, I am trying to scramble repeating characters so that they don't match anymore. But in such a way that i can reverse the outcome later on. Lets say
input_string = "test"
. In "test", t=1 e=2 s=3 t=4 and so on. I need the code to say, "Okay, 't' is in slot '1', so it should be lets say 'Q'. Okay, 't' is also in slot '4', so that should be 'f'." Do you see what i mean?

Answer Source

With the help of Mark Tolonen in this post, I was able to come up with a solution. The following example only uses 4 dictionaries, while i intend to do more:

# Input String
string = "ttttttttttttttttttttttttttttttt"

# Defining dictionarys for 0-7
dicnums = [{"0":"n","1":"Q","2":"k","3":"W","4":"F","5":"g","6":"9","7":"e","8":"v","9":"3","a":"r","b":"T","c":"c","d":"o","e":"b","f":"y","g":"2","h":"A","i":"i","j":"p","k":"1","l":"P","m":"w","n":"x","o":"s","p":"Y","q":"h","r":"G","s":"7","t":"S","u":"6","v":"K","w":"Z","x":"M","y":"C","z":"J","A":"u","B":"f","C":"j","D":"E","E":"a","F":"H","G":"O","H":"N","I":"l","J":"U","K":"I","L":"V","M":"m","N":"5","O":"R","P":"4","Q":"z","R":"L","S":"0","T":"q","U":"D","V":"8","W":"B","X":"X","Y":"d","Z":"t"}, {"0":"q","1":"6","2":"W","3":"4","4":"J","5":"u","6":"n","7":"T","8":"I","9":"O","a":"V","b":"3","c":"Z","d":"s","e":"R","f":"E","g":"G","h":"P","i":"5","j":"l","k":"e","l":"m","m":"F","n":"t","o":"8","p":"K","q":"L","r":"Y","s":"M","t":"D","u":"j","v":"z","w":"H","x":"g","y":"9","z":"f","A":"0","B":"p","C":"o","D":"d","E":"X","F":"S","G":"k","H":"1","I":"Q","J":"C","K":"U","L":"i","M":"r","N":"w","O":"y","P":"B","Q":"2","R":"x","S":"A","T":"c","U":"7","V":"h","W":"v","X":"N","Y":"b","Z":"a"}, {"0":"x","1":"W","2":"q","3":"B","4":"j","5":"I","6":"E","7":"g","8":"U","9":"e","a":"8","b":"3","c":"5","d":"k","e":"9","f":"N","g":"7","h":"Q","i":"t","j":"r","k":"L","l":"Z","m":"b","n":"n","o":"Y","p":"H","q":"R","r":"6","s":"P","t":"1","u":"S","v":"M","w":"p","x":"l","y":"F","z":"2","A":"c","B":"T","C":"G","D":"h","E":"X","F":"v","G":"s","H":"O","I":"D","J":"4","K":"a","L":"A","M":"m","N":"d","O":"C","P":"f","Q":"V","R":"i","S":"o","T":"u","U":"w","V":"0","W":"K","X":"z","Y":"y","Z":"J"}, {"0":"x","1":"W","2":"q","3":"B","4":"j","5":"I","6":"E","7":"g","8":"U","9":"e","a":"8","b":"3","c":"5","d":"k","e":"9","f":"N","g":"7","h":"Q","i":"t","j":"r","k":"L","l":"H","m":"b","n":"n","o":"Y","p":"Z","q":"R","r":"6","s":"P","t":"1","u":"S","v":"M","w":"p","x":"l","y":"F","z":"2","A":"c","B":"T","C":"G","D":"h","E":"X","F":"v","G":"s","H":"O","I":"D","J":"4","K":"a","L":"A","M":"m","N":"d","O":"C","P":"f","Q":"V","R":"i","S":"o","T":"u","U":"w","V":"0","W":"K","X":"z","Y":"y","Z":"J"}]

# Changing my string characters
string_fin = ''.join(dicnums[i%4][c] for i,c in enumerate(string))

# Print result
print string_fin `

The result is "SD11SD11SD11SD11SD11SD11SD11SD1" if i use all 't's. If i make the dict range broader, and am not using the same characters repeatedly(which i wont be) then the output would be much better. Once combined with the rest of my encryption concept, my program might actually work like i want.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download