Change character based off of its position? Python 2.7

2024/7/4 15:18:49

I have a string on unknown length, contain the characters a-z A-Z 0-9. I need to change each character using their position from Left to Right using a dictionary.

Example:

string = "aaaaaaaa"
def shift_char(text):for i in len(text):# Do Something for each characterreturn output
print shift_char(string)
'adktivep'
Answer

Alright, hopefully this is understandable, otherwise feel free to ask questions.

import random
import string# Letter pool, these are all the possible characters
# you can have in your string
letter_pool = list(string.ascii_letters + string.digits)input_string = "HelloWorld"
string_length = len(input_string)# Generate one random dictionary for each character in string
dictionaries = []for i in range(string_length):# Copy letter_pool to avoid overwriting letter_poolkeys = list(letter_pool)values = list(letter_pool)# Randomise values, (keep keys the same)random.shuffle(values)# This line converts two lists into a dictionaryscrambled_dict = dict(zip(keys, values))# Now each letter (key) maps to a random letter (value)dictionaries.append(scrambled_dict)# Initiate a fresh string to start adding characters to
out_string = ""# Loop though the loop string, record the current place (index) and character each loop
for index, char in enumerate(input_string):# Get the dictionary for this place in the stringdictionary = dictionaries[i]# Get the randomised character from the dictionarynew_char = dictionary[char]# Place the randomised character at the end of the stringout_string += new_char# Print out the random string
print(out_string)

Edit: If you want to only generate the random dictionaries once, and load them in everytime, you can serialise the dictionaries array. My favourite is json.

https://en.xdnf.cn/q/120386.html

Related Q&A

Changing type using str() and int()...how it works

If I do this, I get:>>> x = 1 >>> y = 2 >>> type(x) <class int> >>> type(y) <class str>That all makes sense to me, except that if I convert using:>>…

Caesar cypher in Python

I am new to python and I want to write a program that asks for a line of input (each number will be separated by a single space.) It would use a simple letter-number substitution cipher. Each letter w…

Flatten a list with sublists and strings [duplicate]

This question already has answers here:Flatten an irregular (arbitrarily nested) list of lists(54 answers)How do I make a flat list out of a list of lists?(32 answers)Closed 2 years ago.How can I flat…

Guitar string code in Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…

What is the point of initializing extensions with the flask instance in Flask? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

Nested list doesnt work properly

import re def get_number(element):re_number = re.match("(\d+\.?\d*)", element)if re_number:return float(re_number.group(1))else:return 1.0def getvalues(equation):elements = re.findall("…

How can I increment array with loop?

Ive got two lists:listOne = [33.325556, 59.8149016457, 51.1289412359] listTwo = [2.5929778, 1.57945488999, 8.57262235411]I use len to tell me how many items are in my list:itemsInListOne = int(len(list…

Two Complements Python (with as least bits as possible)

I am trying to output the binary representation of an negative number with the least bytes available each time.Example:-3 -> 101 -10 -> 10110

iterating through a column in dataframe and creating a list with name of the column + str

I have a dataframe with 2 coulmn, i want to iterate through the column headers, use that value and add a string to it and use it as the name of a list.rrresampled=pd.DataFrame() resampled[RAT]=dd1[RAT]…

Python multiple inheritance name clashes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…