Python: Assigning numbering to list of ascii

Question:

Need help with making a encryption python program that encrypts with use of ascii values. I have 1-127 random number generator with no repeats and need to basically assign a value to each one.
Example:
list 1 is (1,2,3…127)
list 2 is (54,60,27…)
I need to get a list or dictionary of (1 : 54 , 2 : 60 , 3 : 27…).

End goal is that after encryption, 54 is assigned to ascii 1 (soh), if the number 54 appears in the encrypted string, then original string had a soh in that slot

I do not know the proper way to assign the random number list a number. I think its dictionary but I am not familiar with dict

Asked By: Addapp

||

Answers:

You can make a dict from 2 lists with:

listsdict = dict(zip(list1, list2))

Additionally then you can iterate through your input string look up the Value like

ascii_value = ord(char)

# Look up the corresponding value in the dictionary using the ASCII value as the key
encrypted_value = dict1[ascii_value]
Answered By: mxwmnn

Welcome to StackOverflow. I urge you to take a look at the How do I ask a good question?, and How to create a Minimal, Reproducible Example pages so that we can point you in the right direction more easily.

You’re correct in thinking that a dictionary would be a suitable tool for this problem.
You can learn all about dict and how it works in the Python docs page about built-in types.
That page has a nifty example that covers what you described perfectly (through the usage of zip):

c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
Answered By: tlgs
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.