Related
This question already has answers here:
How to convert list to string [duplicate]
(3 answers)
Closed 4 months ago.
How do I remove the all the spaces in the end result?
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
x_letters = int(input("How many letters would you like in your
password? "))
x_symbols = int(input(f"How many symbols would you like? "))
x_numbers = int(input(f"How many numbers would you like? "))
import random
password_list = []
for char in range(1, x_letters + 1):
password_list.append(random.choice(letters))
for char in range(1, x_symbols + 1):
password_list.append(random.choice(numbers))
for char in range(1, x_numbers + 1):
password_list.append(random.choice(symbols))
random.shuffle(password_list)
print(" ".join(password_list))
When I input everything it types everything like this:
r 3 a $ w & % 6 1
Last line should be
print("".join(password_list))
Leave no space in the join method attachment
which is print("".join(password_list))
Anything within that string will separate the elements
Is there a "base64.b85decode" function in nodejs?
It uses the following character set -
0123456789
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
!#$%&()*+-;<=>?#^_`{|}~
It's not a regular Ascii85 encoding but a different base85 encoding type.
For example, for "Hello, world!!!!", It should return "NM&qnZ!92pZpv8At50l"
I solved it by using the ascii85 package and using a customized character set -
var ascii85 = require('ascii85');
ascii85.decode(to_decode, ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '!', '#', '$', '%', '&', '(', ')', '*', '+', '-', ';', '<', '=', '>', '?', '#', '^', '_', '`', '{', '|', '}', '~']).toString('ascii');
I am designing a program that will encode. messages imported from a csv. It will do this by converting them their ASCII value, adding 2 to them and then converting them back to characters.
My current problem is that while my code will encode each character in each string the messages are now no longer joined together.
Any help would be appreciated.
My code:
#importing csv file and allowing it to be read from
import csv
ifile = open("messages.csv","rb")
reader= csv.reader(ifile)
#creating lists
plain_text=[]
plain_ascii=[]
encrypted_ascii=[]
encrypted_text=[]
latitude=[]
longitude=[]
#appending csv data to separate lists
for row in reader:
latitude.append(row[0])
longitude.append(row[1])
plain_text.append(row[2])
#encoding messages
encrypted_text=[[chr(ord(ch)+2) for ch in string] for string in plain_text]
print plain_text
print encrypted_text
ifile.close()
The current output:
['A famous Scottish victory in the First War of Scottish Independence - bit.ly/1yIAb8Q', "How high is Scotland's tallest mountain? - bit.ly/1q3Rj6D", 'What is the traditional instrument most often linked with Scotland? - http://#bit.ly/1lNdrk3', "A prickly problem Scotland's national symbol - bit.ly/1q3REpQ", 'Name the largest city in Scotland - bit.ly/T4OEuU']
[['C', '"', 'h', 'c', 'o', 'q', 'w', 'u', '"', 'U', 'e', 'q', 'v', 'v', 'k', 'u', 'j', '"', 'x', 'k', 'e', 'v', 'q', 't', '{', '"', 'k', 'p', '"', 'v', 'j', 'g', '"', 'H', 'k', 't', 'u', 'v', '"', 'Y', 'c', 't', '"', 'q', 'h', '"', 'U', 'e', 'q', 'v', 'v', 'k', 'u', 'j', '"', 'K', 'p', 'f', 'g', 'r', 'g', 'p', 'f', 'g', 'p', 'e', 'g', '"', '/', '"', 'd', 'k', 'v', '0', 'n', '{', '1', '3', '{', 'K', 'C', 'd', ':', 'S'], ['J', 'q', 'y', '"', 'j', 'k', 'i', 'j', '"', 'k', 'u', '"', 'U', 'e', 'q', 'v', 'n', 'c', 'p', 'f', ')', 'u', '"', 'v', 'c', 'n', 'n', 'g', 'u', 'v', '"', 'o', 'q', 'w', 'p', 'v', 'c', 'k', 'p', 'A', '"', '/', '"', 'd', 'k', 'v', '0', 'n', '{', '1', '3', 's', '5', 'T', 'l', '8', 'F'], ['Y', 'j', 'c', 'v', '"', 'k', 'u', '"', 'v', 'j', 'g', '"', 'v', 't', 'c', 'f', 'k', 'v', 'k', 'q', 'p', 'c', 'n', '"', 'k', 'p', 'u', 'v', 't', 'w', 'o', 'g', 'p', 'v', '"', 'o', 'q', 'u', 'v', '"', 'q', 'h', 'v', 'g', 'p', '"', 'n', 'k', 'p', 'm', 'g', 'f', '"', 'y', 'k', 'v', 'j', '"', 'U', 'e', 'q', 'v', 'n', 'c', 'p', 'f', 'A', '"', '/', '"', 'j', 'v', 'v', 'r', '<', '1', '1', 'd', 'k', 'v', '0', 'n', '{', '1', '3', 'n', 'P', 'f', 't', 'm', '5'], ['C', '"', 'r', 't', 'k', 'e', 'm', 'n', '{', '"', 'r', 't', 'q', 'd', 'n', 'g', 'o', '"', 'U', 'e', 'q', 'v', 'n', 'c', 'p', 'f', ')', 'u', '"', 'p', 'c', 'v', 'k', 'q', 'p', 'c', 'n', '"', 'u', '{', 'o', 'd', 'q', 'n', '"', '/', '"', 'd', 'k', 'v', '0', 'n', '{', '1', '3', 's', '5', 'T', 'G', 'r', 'S'], ['P', 'c', 'o', 'g', '"', 'v', 'j', 'g', '"', 'n', 'c', 't', 'i', 'g', 'u', 'v', '"', 'e', 'k', 'v', '{', '"', 'k', 'p', '"', 'U', 'e', 'q', 'v', 'n', 'c', 'p', 'f', '"', '/', '"', 'd', 'k', 'v', '0', 'n', '{', '1', 'V', '6', 'Q', 'G', 'w', 'W']]
You need to join the inner lists.
[''.join(chr(ord(ch)+2) for ch in string) for string in plain]
I am having trouble finishing this code. My original plan was to accept a long string as input , for example '... ---.. -. -.. .... . .-.. .--.' and then be able to use morse_code = [code_input.split(' ')] to separate them and run them individually but I am either getting the first character returning from the index or no return at all so can anyone help me mend this or help lead me in the way of a simpler solution?
Thanks to all for any help!
#morse code converter
def main():
code_input = get_input()
morse_code(code_input)
def get_input():
return input('Enter morse code: ')
def morse_code(code_input):
morse_code = [code_input]
convert = ['--..--', '.-.-.-', '..--..', '-----', '.----', '..---', '...--', '....-', '.....', '-....', '--...', '---..',
'----.', '.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---' ,'-.-', '.-..', '--', '-.', '---',
'.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.-', '--..']
new = [',', '.', '?', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',]
print(morse_code)
length = len(morse_code)
for x in range(length):
for value in range(39):
if morse_code[x] == convert[value]:
print(new[value])
main()
Your idea to use split should work just fine:
>>> '... ---.. -. -.. .... . .-.. .--.'.split()
['...', '---..', '-.', '-..', '....', '.', '.-..', '.--.']
For the translation, I would use a dictionary instead of a list:
morse2text = {'...': 's', '---': 'o'}
To avoid key errors, I would a do a "safe lookup" using the dictionary get() method:
print( morse2text.get(m, m) )
Here is all the code put together (though with an incomplete dictionary) in case you want to build-off of a working example:
morse2text = {
'.-': 'a',
'-..': 'd',
'.': 'e',
'....': 'h',
'..': 'i',
'-.': 'n',
'---': 'o',
'...': 's',
'-': 't',
}
s = '... ---.. -. -.. .... . .-.. .--.'
for m in s.split():
print( morse2text.get(m, m) )
This should work for you:
def morse_code(code_input):
morse_codes = code_input.split(' ')
convert = ['--..--', '.-.-.-', '..--..', '-----', '.----', '..---', '...--', '....-', '.....', '-....', '--...', '---..',
'----.', '.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---' ,'-.-', '.-..', '--', '-.', '---',
'.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.-', '--..']
new = [',', '.', '?', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',]
print(morse_codes)
code = ''
for x in morse_codes:
index = convert.index(x)
code+=new[index]
return code
print morse_code('... ---.. -. -.. .... . .-.. .--.')
Output:
['...', '---..', '-.', '-..', '....', '.', '.-..', '.--.']
'S8NDHELP'
As the Legendary Raymond mentioned, dict type is a better option.
Try dict(zip(convert,new)) to make a dictionary for conversion.
d = {'--..--': ',', '....-': '4', '.....': '5', '-...': 'B', '-..-': 'X',
'.-.': 'R', '--.-': 'Q', '--..': 'Z', '.--': 'W', '..---': '2',
'.-': 'A', '..': 'I', '-.-.': 'C', '...--': '3', '-': 'T', '.': 'E',
'.-..': 'L', '...': 'S', '..-': 'U', '..--..': '?', '.----': '1',
'.--.': 'P', '-----': '0', '-.-': 'Y', '-..': 'D', '----.': '9',
'-....': '6', '.---': 'J', '---': 'O', '.-.-.-': '.', '--': 'M',
'-.': 'N', '....': 'H', '---..': '8', '...-': 'V', '--...': '7',
'--.': 'G', '..-.': 'F'}
To use the dict:
translation = ''
for c in morse_codes:
translation += d[c]
I have two, unequal length lists. The first list contains variable names for a longitudinal study, the other contains suffixes for these variables. The user supplies a CSV from which the variable names are read, and then is prompted to enter the (n) number of iterations of these variables, and the names of the n number of suffixes
Here is a fake example of what I mean
Number of iterations: 2
Suffix1: pre
Suffix2: 6month
List 1:
['mood1', 'mood2', 'mood3', 'dep1', 'dep2', 'dep3']
List 2:
['pre', '6month']
Desired concatenation:
['mood1_pre', 'mood2_pre', 'mood3_pre', 'dep1_pre', 'dep2_pre', 'dep3_pre', 'mood1_6month', ..., 'dep3_6month']
I have the program working fully, except the output splits each letter of the concatenated list into its own element, for example:
How many iterations of the variables do you need?: 3
Variable Suffix 1: pre
Variable Suffix 2: 6m
Variable Suffix 3: 12m
['B', 'o', 'b', '_', 'p', 'r', 'e', 'J', 'o', 'e', '_', 'p', 'r', 'e', 'J', 'i',
'm', '_', 'p', 'r', 'e', 'A', '_', 'p', 'r', 'e', 'B', '_', 'p', 'r', 'e', 'C',
'_', 'p', 'r', 'e', '1', '_', 'p', 'r', 'e', '2', '_', 'p', 'r', 'e', '3', '_',
'p', 'r', 'e', '1', '4', '_', 'p', 'r', 'e', 'B', 'o', 'b', '_', '6', 'm', 'J',
'o', 'e', '_', '6', 'm', 'J', 'i', 'm', '_', '6', 'm', 'A', '_', '6', 'm', 'B',
'_', '6', 'm', 'C', '_', '6', 'm', '1', '_', '6', 'm', '2', '_', '6', 'm', '3',
'_', '6', 'm', '1', '4', '_', '6', 'm', 'B', 'o', 'b', '_', '1', '2', 'm', 'J',
'o', 'e', '_', '1', '2', 'm', 'J', 'i', 'm', '_', '1', '2', 'm', 'A', '_', '1',
'2', 'm', 'B', '_', '1', '2', 'm', 'C', '_', '1', '2', 'm', '1', '_', '1', '2',
'm', '2', '_', '1', '2', 'm', '3', '_', '1', '2', 'm', '1', '4', '_', '1', '2',
'm']
I am using this to make the new list
newvarlist.extend((varlist[vars] + '_' + varsuffix[j]))
Here is one way to do it using list comprehension:
['{}_{}'.format(a, b) for b in b_list for a in a_list]
Demo:
>>> a_list = ['mood1', 'mood2', 'mood3', 'dep1', 'dep2', 'dep3']
>>> b_list = ['pre', '6month']
>>> result = ['{}_{}'.format(a, b) for b in b_list for a in a_list]
>>> result
['mood1_pre', 'mood2_pre', 'mood3_pre', 'dep1_pre', 'dep2_pre', 'dep3_pre', 'mood1_6month', 'mood2_6month', 'mood3_6month', 'dep1_6month', 'dep2_6month', 'dep3_6month']
If you are flexible on the ordering of the final list:
from itertools import product, imap
l1 = ['mood1', 'mood2', 'mood3', 'dep1', 'dep2', 'dep3']
l2 = ['pre', '6month']
x = list(imap('_'.join, product(l1, l2)))
This produces ['mood1_pre', 'mood1_6month', ...] rather than ['mood1_pre', 'mood2_pre', ...].