Python: how to move the characters via index?

Question:

I have 2 arrays encryted_message = [] and index_shuffle = []. The encryted_message = [] is the array that will take in the user input and split the words into separate characters, the index_shuffle = [] will keep track of the index number of each characters in encryted_message = []

I have already done the programming that takes the user input and adds it to the 2 arrays as mentioned above.

encryted_message = []
index_shuffle = []

User_input = input("Type your message here: ")

char_split = list(map(list,User_input))

for char in char_split:
    for charfinal in char:
        encryted_message.append(charfinal)
        
for i,element in enumerate (encryted_message):
        index_shuffle.append(i)

This is the output of the code above:

Type your message here: hello
['h', 'e', 'l', 'l', 'o']
[0, 1, 2, 3, 4]

if you notice that the index_shuffle = [] index numbers will be determined depending on how many characters are in the encryted_message = [].

What I would like the outcome of the next step to be is this:

the index of characters in encryted_message = [] ([‘h’, ‘e’, ‘l’, ‘l’, ‘o’]) will need to be shuffled around based on the index_shuffle = [] ([0, 1, 2, 3, 4])

I am trying to encrypt the message using the index_shuffle = []. Which I am not sure how to do.

What I mean by shuffle is this:

[‘h’, ‘e’, ‘l’, ‘l’, ‘o’] – normal text
[‘o’, ‘e’, ‘l’, ‘l’, ‘h’] – shuffled based on index_shuffle = [] (This example is not accurate but to it’s to give you an idea of what I am trying to achieve)

Asked By: CentralDev

||

Answers:

Not entirely sure I understand the question, but perhaps you’re looking for something like this:

text = 'hello'

letter_index = dict(zip(range(len(text)), text))

original_index = list(range(len(text)))

shuffled_index = random.sample(original_index, len(text))

shuffled_string = ''.join([letter_index[index] for index in shuffled_index])
Answered By: Michael Cao
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.