Pandas array to columns ( need to convert alphabets into words) - python-3.x

I have data in the below array format, I need to convert this array
these alphabets into words e.g.
(' ', 'a', 'd', 'e', 'l', 'o', 'r', 't') = 'adelort'
how i can do this
Array =[(' ', 'a', 'd', 'e', 'l', 'o', 'r', 't'),
(' ', 'a', 'd', 'e', 'l', 'o', 'r', 't'),
(' ', 'e', 'i', 'o', 't', 'v'),
('d', 'e', 'g', 'i', 'n', 'r', 't'),
('d', 'e', 'g', 'i', 'n', 'r', 't'),
('a', 'd', 'e', 'i', 'l', 'm', 'n', 't')]
Getting above array while working on an NLP problem, please refer below code:
xtest_tfidf = tfidf_vectorizer.transform(pred_test)
y_pred_test = clf.predict(xtest_tfidf)
multilabel_binarizer.inverse_transform(y_pred_test)

Not sure about the NLP part but you asked how to convert the array into words:
Array =[(' ', 'a', 'd', 'e', 'l', 'o', 'r', 't'),
(' ', 'a', 'd', 'e', 'l', 'o', 'r', 't'),
(' ', 'e', 'i', 'o', 't', 'v'),
('d', 'e', 'g', 'i', 'n', 'r', 't'),
('d', 'e', 'g', 'i', 'n', 'r', 't'),
('a', 'd', 'e', 'i', 'l', 'm', 'n', 't')]
words = ["".join(x).strip() for x in Array]
yields
['adelort', 'adelort', 'eiotv', 'deginrt', 'deginrt', 'adeilmnt']

Related

How to join array into string in Julia

How can I join [5, 'N', 'K', 'r', 9, 'j', 'K', '(', 'E', 't'] into making it a single string like this "5NKr9jK(Et"
Just join it :):
julia> join([5, 'N', 'K', 'r', 9, 'j', 'K', '(', 'E', 't'])
"5NKr9jK(Et"
The individual elements of the vector are converted to string using the print function.

python 3 how to generate multiple random element in list for loops

I'm doing a coding exercise and it's to build a password generator. I understand I need to utilize the for loop with the list containing the elements but I'm having trouble getting multiple random elements. If the user input is 5, I'm able to generate a random letter and 5 times of the same element but I can't get it to generate 5 different elements. What code do I need to utilize to generate random elements depending on user input? I know my code and logic is incorrect but I can't figure out how else to get around this. Any feedback is much appreciated, thank you.
import random
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']
nr_letters= int(input("How many letters would you like in your password?\n"))
for letter in letters:
random_letter = random.choice(letters) * nr_letters
print(random_letter)
There could be better ways - I've just used your code.
The for loop you are using is redundant.
Can do something like -
import random
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']
nr_letters= int(input("How many letters would you like in your password?\n"))
random_letter=''
for i in range (nr_letters):
random_letter += random.choice(letters)
print(random_letter)
You actually don't have to use for loop to get your desired password.
import random
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']
nr_letters= int(input("How many letters would you like in your password?\n"))
random_letter = "".join(random.choices(letters, k= nr_letters))
print(random_letter)
but if you must use loop, just pass the above code under loop as you wish. Happy coding.

Password Generator - implementation with minimum password length constraint e.g. password length = 8 (in python)

I am not able to modify the code when I implement minimum password length like minimum length must be 8 I tried using a while loop but code is not running as expected Please help me in this case
import random
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 = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your
password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
password = []
password.extend(random.sample(letters, nr_letters))
password.extend(random.sample(symbols, nr_symbols))
password.extend(random.sample(numbers, nr_numbers))
random.shuffle(password)
finalPassword = ""
print(f"Here is you password: {finalPassword.join(password)}")
Simply keep the input statement within a while loop that checks your criteria:
import random
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 = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters = 0
nr_symbols = 0
nr_numbers = 0
while nr_letters + nr_symbols + nr_numbers < 8:
nr_letters= int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
password = []
password.extend(random.sample(letters, nr_letters))
password.extend(random.sample(symbols, nr_symbols))
password.extend(random.sample(numbers, nr_numbers))
random.shuffle(password)
finalPassword = ""
print(f"Here is you password: {finalPassword.join(password)}")

Iterating through list of strings returns individual characters

I have a list of 3 letter strings that match the keys of a dictionary in my program. I want to translate each 3 letter string using the dictionary, so I wrote a for loop. Instead of translating each element in the list, it translates each single character in each 3 letter element, which accomplishes nothing because there is no pair in the dictionary. I feel like I'm forgetting something basic about for loops and lists, but I can't figure out how to get this to iterate and translate how I want. Code Below:
rnaCodonTable = {
# RNA codon table
# U
'UUU': 'F', 'UCU': 'S', 'UAU': 'Y', 'UGU': 'C', # UxU
'UUC': 'F', 'UCC': 'S', 'UAC': 'Y', 'UGC': 'C', # UxC
'UUA': 'L', 'UCA': 'S', 'UAA': 'STOP', 'UGA': 'STOP', # UxA
'UUG': 'L', 'UCG': 'S', 'UAG': 'STOP', 'UGG': 'W', # UxG
# C
'CUU': 'L', 'CCU': 'P', 'CAU': 'H', 'CGU': 'R', # CxU
'CUC': 'L', 'CCC': 'P', 'CAC': 'H', 'CGC': 'R', # CxC
'CUA': 'L', 'CCA': 'P', 'CAA': 'Q', 'CGA': 'R', # CxA
'CUG': 'L', 'CCG': 'P', 'CAG': 'Q', 'CGG': 'R', # CxG
# A
'AUU': 'I', 'ACU': 'T', 'AAU': 'N', 'AGU': 'S', # AxU
'AUC': 'I', 'ACC': 'T', 'AAC': 'N', 'AGC': 'S', # AxC
'AUA': 'I', 'ACA': 'T', 'AAA': 'K', 'AGA': 'R', # AxA
'AUG': 'M', 'ACG': 'T', 'AAG': 'K', 'AGG': 'R', # AxG
# G
'GUU': 'V', 'GCU': 'A', 'GAU': 'D', 'GGU': 'G', # GxU
'GUC': 'V', 'GCC': 'A', 'GAC': 'D', 'GGC': 'G', # GxC
'GUA': 'V', 'GCA': 'A', 'GAA': 'E', 'GGA': 'G', # GxA
'GUG': 'V', 'GCG': 'A', 'GAG': 'E', 'GGG': 'G' # GxG
}
self.aaList = []
self.codonList = ['AUG', 'AGG', 'CUG', 'AAG', 'AUA', 'AGG', 'ACA', 'GAC', 'GGC', 'GCC', 'GCC', 'CAG', 'CAA', 'CAG', 'CAG', 'GCG', 'GAC', 'UGG', 'CGG', 'GAC', 'UGC', 'UUC', 'AUC', 'CGC', 'GCC', 'GUC', 'GUC', 'GAG', 'AUG', 'CCG', 'GCG', 'GAC', 'UGG', 'GGC', 'AUG', 'GCG', 'AUA', 'AUC', 'AAG', 'GCC', 'AUG', 'CCC', 'CAG', 'GAG', 'AUG', 'GUA', 'AAC', 'GAG', 'CUG', 'UUA', 'CAA', 'AGC', 'CGA', 'AAC', 'GAC', 'CCC', 'UAC', 'UAC', 'AAA', 'UUC', 'GCG', 'CUU', 'CUG', 'CUA', 'CUG', 'CAG', 'AGG', 'GCA', 'CAG', 'AAA', 'UAA']
for codon in self.codonList[:]:
self.aaList += codon.translate(self.rnaCodonTable)
print (self.aaList)
I was expecting an output of ['M', 'R', 'L',...] translating each 3 letter string according to the dictionary, but instead I get:
['A', 'U', 'G', 'A', 'G', 'G', 'C', 'U', 'G', 'A', 'A', 'G', 'A', 'U', 'A', 'A', 'G', 'G', 'A', 'C', 'A', 'G', 'A', 'C', 'G', 'G', 'C', 'G', 'C', 'C', 'G', 'C', 'C', 'C', 'A', 'G', 'C', 'A', 'A', 'C', 'A', 'G', 'C', 'A', 'G', 'G', 'C', 'G', 'G', 'A', 'C', 'U', 'G', 'G', 'C', 'G', 'G', 'G', 'A', 'C', 'U', 'G', 'C', 'U', 'U', 'C', 'A', 'U', 'C', 'C', 'G', 'C', 'G', 'C', 'C', 'G', 'U', 'C', 'G', 'U', 'C', 'G', 'A', 'G', 'A', 'U', 'G', 'C', 'C', 'G', 'G', 'C', 'G', 'G', 'A', 'C', 'U', 'G', 'G', 'G', 'G', 'C', 'A', 'U', 'G', 'G', 'C', 'G', 'A', 'U', 'A', 'A', 'U', 'C', 'A', 'A', 'G', 'G', 'C', 'C', 'A', 'U', 'G', 'C', 'C', 'C', 'C', 'A', 'G', 'G', 'A', 'G', 'A', 'U', 'G', 'G', 'U', 'A', 'A', 'A', 'C', 'G', 'A', 'G', 'C', 'U', 'G', 'U', 'U', 'A', 'C', 'A', 'A', 'A', 'G', 'C', 'C', 'G', 'A', 'A', 'A', 'C', 'G', 'A', 'C', 'C', 'C', 'C', 'U', 'A', 'C', 'U', 'A', 'C', 'A', 'A', 'A', 'U', 'U', 'C', 'G', 'C', 'G', 'C', 'U', 'U', 'C', 'U', 'G', 'C', 'U', 'A', 'C', 'U', 'G', 'C', 'A', 'G', 'A', 'G', 'G', 'G', 'C', 'A', 'C', 'A', 'G', 'A', 'A', 'A', 'U', 'A', 'A']
How do I make the translation affect whole strings within a list, rather than each character of the strings?
I removed all the selfs for simplicity, you were close, your iteration was correct, but you need to iterate over each object in the list.
And then using that object to find the corresponding value in the dictionary you defined. Fixed your indentation with print which was incorrect.
Also you do not need to use translate, and I used append to add the values to the resulting list.
Hope that helps :)
rnaCodonTable = {
# RNA codon table
# U
'UUU': 'F', 'UCU': 'S', 'UAU': 'Y', 'UGU': 'C', # UxU
'UUC': 'F', 'UCC': 'S', 'UAC': 'Y', 'UGC': 'C', # UxC
'UUA': 'L', 'UCA': 'S', 'UAA': 'STOP', 'UGA': 'STOP', # UxA
'UUG': 'L', 'UCG': 'S', 'UAG': 'STOP', 'UGG': 'W', # UxG
# C
'CUU': 'L', 'CCU': 'P', 'CAU': 'H', 'CGU': 'R', # CxU
'CUC': 'L', 'CCC': 'P', 'CAC': 'H', 'CGC': 'R', # CxC
'CUA': 'L', 'CCA': 'P', 'CAA': 'Q', 'CGA': 'R', # CxA
'CUG': 'L', 'CCG': 'P', 'CAG': 'Q', 'CGG': 'R', # CxG
# A
'AUU': 'I', 'ACU': 'T', 'AAU': 'N', 'AGU': 'S', # AxU
'AUC': 'I', 'ACC': 'T', 'AAC': 'N', 'AGC': 'S', # AxC
'AUA': 'I', 'ACA': 'T', 'AAA': 'K', 'AGA': 'R', # AxA
'AUG': 'M', 'ACG': 'T', 'AAG': 'K', 'AGG': 'R', # AxG
# G
'GUU': 'V', 'GCU': 'A', 'GAU': 'D', 'GGU': 'G', # GxU
'GUC': 'V', 'GCC': 'A', 'GAC': 'D', 'GGC': 'G', # GxC
'GUA': 'V', 'GCA': 'A', 'GAA': 'E', 'GGA': 'G', # GxA
'GUG': 'V', 'GCG': 'A', 'GAG': 'E', 'GGG': 'G' # GxG
}
aaList = []
codonList = ['AUG', 'AGG', 'CUG', 'AAG', 'AUA', 'AGG', 'ACA', 'GAC', 'GGC', 'GCC', 'GCC', 'CAG', 'CAA', 'CAG', 'CAG', 'GCG', 'GAC', 'UGG', 'CGG', 'GAC', 'UGC', 'UUC', 'AUC', 'CGC', 'GCC', 'GUC', 'GUC', 'GAG', 'AUG', 'CCG', 'GCG', 'GAC', 'UGG', 'GGC', 'AUG', 'GCG', 'AUA', 'AUC', 'AAG', 'GCC', 'AUG', 'CCC', 'CAG', 'GAG', 'AUG', 'GUA', 'AAC', 'GAG', 'CUG', 'UUA', 'CAA', 'AGC', 'CGA', 'AAC', 'GAC', 'CCC', 'UAC', 'UAC', 'AAA', 'UUC', 'GCG', 'CUU', 'CUG', 'CUA', 'CUG', 'CAG', 'AGG', 'GCA', 'CAG', 'AAA', 'UAA']
for codon in codonList:
aaList.append(rnaCodonTable[codon])
print (aaList)
From docs on string.translate:
Delete all characters from s that are in deletechars (if present), and then translate the characters using table, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. If table is None, then only the character deletion step is performed.
...which is something completely different from what you're doing. This is what you should be doing:
aaList = ""
for codon in self.codonList:
self.aaList += self.rnaCodonTable[codon]

How to enable arabic slug in htaccess?

I have a multi-languages website, and I'm trying to create a friendly URL. In my database, I have the slug field. When the article's title is in english the slug appear in url and redirection works fine. but when the title is arabic the slug appear and the redirection shows "Object not found" page.
what seems to be the problem guys ? please help I'm stack.
Most likely the issue is your rewriting rule. It explicitly is crafter such that it only gets applied for requests that consist of only ascii characters, an underscore or a hyphen in the slug part of the URL. That obviously won't match arabic characters in the URL. So you have to change your rule to accept more or less anything expect very special characters:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUE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', 'ŧ', 'Ũ', 'ũ', 'Ū', 'ū', 'Ŭ', 'ŭ', 'Ů', 'ů', 'Ű', 'ű', 'Ų', 'ų', 'Ŵ', 'ŵ', 'Ŷ',
'ŷ', 'Ÿ', 'Ź', 'ź', 'Ż', 'ż', 'Ž', 'ž', 'ſ', 'ƒ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ǎ', 'ǎ', 'Ǐ', 'ǐ',
'Ǒ', 'ǒ', 'Ǔ', 'ǔ', 'Ǖ', 'ǖ', 'Ǘ', 'ǘ', 'Ǚ', 'ǚ', 'Ǜ', 'ǜ', 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ');
$b = array('A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O',
'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c',
'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u',
'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D',
'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g',
'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K',
'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o',
'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S',
's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W',
'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i',
'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
$lettersNumbersSpacesHyphens = '/[^\-\s\pN\pL]+/u';
$spacesDuplicateHyphens = '/[\-\s]+/';
// remove anithing that isn't latters, spaces, numbers, hyphens
$slug = preg_replace($lettersNumbersSpacesHyphens, '', mb_strtolower($slug, 'utf-8'));
// remove spaces and duplicate hyphens
$slug = preg_replace($spacesDuplicateHyphens, '-', $slug);
// trim left and right hyphens, remove any left over hyphens
$slug = trim($slug, '-');
//replace all accent chars
$slug = str_replace($a, $b, $slug);
$slug = urlencode($slug);
return $slug;
}
and here are the .htaccess lines:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([0-9]+)/([a-zA-Z0-9_-]+)/?$ article.php?id_art=$1 [NC,L]

Resources