Related
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.
I have a list which looks like seen=['poll','roll','toll','told']
I need to compare characters from each of the elements from that list.
When I try to strip those charcters using
for i in range(len(seen)):
chain1=[]
for j in range(len(seen)):
chain1.append(seen[i][j])
print(chain1)
I get an output like this
['p', 'o', 'l', 'l']
['r', 'o', 'l', 'l']
['t', 'o', 'l', 'l']
['t', 'o', 'l', 'd']
Since these are all different lists I cant seem to iterate over them.
My thinking is, if I can manage to get those lists into a single list of list I can do my iterations.
Any suggestions on how to make it into a list of list or some other way to iterate over those words?
you can merge it like below:
seen=['poll','roll','toll','told']
alist=[]
for i in seen:
chain=[]
for j in i:
chain.append(j)
alist.append(chain)
print(alist)
Output:
[['p', 'o', 'l', 'l'], ['r', 'o', 'l', 'l'], ['t', 'o', 'l', 'l'], ['t', 'o', 'l', 'd']]
Im trying to sort a list of objects based on frequency of occurrence (increasing order) of characters. Im seeing that the sort behaves differently if list has numbers versus characters. Does anyone know why this is happening?
Below is a list of numbers sorted by frequency of occurrence.
# Sort list of numbers based on increasing order of frequency
nums = [1,1,2,2,2,3]
countMap = collections.Counter(nums)
nums.sort(key = lambda x: countMap[x])
print(nums)
# Returns correct output
[3, 1, 1, 2, 2, 2]
But If I sort a list of characters, the order of 'l' and 'o' is incorrect in the below example:
# Sort list of characters based on increasing order of frequency
alp = ['l', 'o', 'v', 'e', 'l', 'e', 'e', 't', 'c', 'o', 'd', 'e']
countMap = collections.Counter(alp)
alp.sort(key = lambda x: countMap[x])
print(alp)
# Returns Below output - characters 'l' and 'o' are not in the correct sorted order
['v', 't', 'c', 'd', 'l', 'o', 'l', 'o', 'e', 'e', 'e', 'e']
# Expected output
['v', 't', 'c', 'd', 'l', 'l', 'o', 'o', 'e', 'e', 'e', 'e']
Sorting uses stable sort - that means if you have the same sorting criteria for two elements they keep their relative order/positioning (here it being the amount of 2 for both of them).
from collections import Counter
# Sort list of characters based on increasing order of frequency
alp = ['l', 'o', 'v', 'e', 'l', 'e', 'e', 't', 'c', 'o', 'd', 'e']
countMap = Counter(alp)
alp.sort(key = lambda x: (countMap[x], x)) # in a tie, the letter will be used to un-tie
print(alp)
['c', 'd', 't', 'v', 'l', 'l', 'o', 'o', 'e', 'e', 'e', 'e']
This fixes it by using the letter as second criteria.
To get your exact output you can use:
# use original position as tie-breaker in case counts are identical
countMap = Counter(alp)
pos = {k:alp.index(k) for k in countMap}
alp.sort(key = lambda x: (countMap[x], pos[x]))
print(alp)
['v', 't', 'c', 'd', 'l', 'l', 'o', 'o', 'e', 'e', 'e', 'e']
See Is python's sorted() function guaranteed to be stable? or https://wiki.python.org/moin/HowTo/Sorting/ for details on sorting.
This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 2 years ago.
Say I have a string that is a sentence, i.e. text = 'Say I have a string that is a sentence' ; is there a method that can be called on text to split the assigned value for string into individual characters, so a list of each individual index I suppose?
Your string already is a sequence of separate characters that can be indexed like you can with a list.
text = 'Say I have a string that is a sentence'
text[0]
>>> S
text[4]
>>> I
No need to use a fancy function for this.
But if you, for some reason need a variable of type List, you can use list(text).
list(text)
>>> ['S', 'a', 'y', ' ', 'I', ' ', 'h', 'a', 'v', 'e', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 't', 'h', 'a', 't', ' ', 'i', 's', ' ', 'a', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
list the whole string straight away. drop an example next time
answer = 'Is this your what you are talking about'
list(anwser)
#output
'I', 's', ' ', 't', 'h', 'i', 's', ' ', 'w', 'h', 'a', 't', ' ', 'y', 'o', 'u', ' ', 'a', 'r', 'e', ' ', 't', 'a', 'l', 'k', 'i', 'n', 'g', ' ', 'a', 'b', 'o', 'u', 't']
string='Here is what you are looking for';
print(list(string));
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']