0

Create a string from random list/dict items? Python

Say you have a list: a = ["1", "2", "3", "4", "5"] What code/function is needed to get this output from that list: "12345" Now how would I do the same thing but this time the ouput is randomly shuffled like this: "23514" Now how would I (in a sense) reverse this process and check each character of a single string to see if its in a list or dictionary and return a value if it is? Hope that makes sense.

4th Jun 2017, 11:29 PM
Spock
Spock - avatar
7 Answers
+ 5
from random import shuffle a = ['1','2','3','4','5'] print(''.join(a)) shuffle(a) print(''.join(a))
5th Jun 2017, 2:46 AM
visph
visph - avatar
+ 4
import random # start by right write litteral dict by using colon ( : ) as separator from key/walue, not equal sign ( = ) letters = { "a": ["&", "^" ], "b": ["3", "|" ], "c": ["U", "_" ] } # improve your data structure by rather write: letters = { "a": "&^", "b": "3|", "c": "U_" } # as string are particular type of list, considering each item is a once character long string ^^ def get_rand_enc_letter(letter): if letter in letters.keys(): return random.choice(letters[letter]) # implicit if letter not in letters ( else ) case behaviour: return letter def encrypt(str): enc_str = "" for letter in str: enc_str += get_rand_enc_letter(letter) return enc_str def decrypt(str): dec_str = "" for enc_letter in str: notfind = True for key, val in letters.items(): if enc_letter in val: dec_str += key notfind = False; break # case of not finded encrypted letter: if notfind: dec_str += enc_letter return dec_str n = 10; otxt = "abba abacababa bac" print("original text: "+otxt) while (n): n -=1 etxt = encrypt(otxt) print("encrypted text: "+etxt) dtxt = decrypt(etxt) print("decrypted text: "+dtxt); # well so, you can shorthand a few by using list comprehension as suggested by @richard, but with less readability # and anyway, even I treat case of character not in keys of your dict, it will be artefacts if: # - in text to encrypt there are a character not in keys of your dict, but present in values of dict: it will be decoded to a not corresponding letter (key) # - each values letter is unique among all encrypted letters in all values: it will be decoded by first encountered, not necessarly the expected letter (key)
5th Jun 2017, 7:13 AM
visph
visph - avatar
0
I tried it Dendi.. , in python 3 had no effect. The output of your code was 1 2
5th Jun 2017, 12:51 AM
LordHill
LordHill - avatar
0
Well crap, I guess I didnt ask the right questions. I'm still confused. Ok, what I want to do is create a script that encodes a message. And then decodes it if need be. I started by creating a dictionary assigning each letter as a key and then a list of 2 different characters as a value for that key: letters = { "a" = ["&", "^" ], "b" = ["3", "|" ], "c" = ["U", "_" ], etc... } so what I was trying to do was have the script ask for input.. lets say that input is "cab". the script then interprets each letter as a seperate key and returns a random value from the list designated to that key.. joins them and prints them as a single string. So "cab" would translate to a code that looks like this: "U^3" or maybe "_&|" Then I wanted to be able to enter a message that was encoded with the same dictionary and have it check the values and return the key associated with said value which would give you back the decoded message. Now I'm not looking for a solution and I'm certainly not looking to have anyone write the script for me.. Thats pointless as I was doing this to practice and learn. So... dont give me a direct solution.. just point me in the right direction please.
5th Jun 2017, 3:53 AM
Spock
Spock - avatar
0
# you may find looking at comprehensions, # the random module and ''.join() useful # as a starting point for the idea. import random letters = { "a":["&", "^" ], "b":["3", "|" ], "c":["U", "_" ]} user_input2 = 'cab' encoded2 = ''.join(random.choice(letters[x]) for x in user_input2) print('encoded as well',encoded2) # you could also do it by index position comparision over # multiple strings rather than a dictionary # if you are just looking to practice.
5th Jun 2017, 6:49 AM
richard
- 1
a = ["1", "2", "3", "4", "5"] //These 2 give you "12345" for i in range(len(a)): print(a[i], end="") print() #Or b="" for i in range(len(a)): b=b+a[i] print(b) # This one gives you a random order import random b="" for i in range(len(a)): run=True while run: num=random.randint(0,len(a)) if str(num) in a and str(num) not in b: b=b+str(num) run=False print(b) # This one Checks if the number is in The list then adds it to the string c=["1","3","7"] for i in range(10): if str(i) in c: print(str(i), end="")
4th Jun 2017, 11:47 PM
LordHill
LordHill - avatar
- 1
In python 2, I use comma after printed variable like this ... li =["1","2"] for i in li: print i, maybe it work too in python 3. and as alt, of course you can join them first with .join("") and print the string.
5th Jun 2017, 12:48 AM
Dendi Handian
Dendi Handian - avatar