So I'm trying to make a random string generator, of four characters which goes as a consonant, a vowel, a consonant, and then a vowel. I don't want any consonants to be used more than once. etc. kajo or qyzu. However, I'm having trouble with the random.choice() method which is not expected. When I run the program, I get the below error:
File "C:\User\Documents\Coding\Python\string_generator.py", line 6, in <module>
i = random.choice(consonants)
File "C:\User\AppData\Local\Programs\Python\Python39\lib\random.py", line 346, in choice
return seq[self._randbelow(len(seq))]
IndexError: list index out of range
My code is below:
import random
vowels = 'aeiouy'
consonants = ['b','c','d','f','g','h','j','k','l','m','m','n','p','q','q','r','s','t','v','v','v','v','w','x','x','x','x','z','z','z','z']
for num in range(20):
i = random.choice(consonants)
word = i
for x in consonants:
if x == i:
consonants.remove(x)
word += random.choice(vowels)
i = random.choice(consonants)
word += i
for x in consonants:
if x == i:
consonants.remove(x)
word += random.choice(vowels)
As juanpa.arrivillaga mentioned, the list is empty on one of the calls to random.choice(consonants) in your for loop. You can see this by adding print(consonants) as the first line within the loop:
Output:
['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'm', 'n', 'p', 'q', 'q', 'r', 's', 't', 'v', 'v', 'v', 'v', 'w', 'x', 'x', 'x', 'x', 'z', 'z', 'z', 'z']
['b', 'c', 'd', 'f', 'g', 'j', 'k', 'l', 'm', 'm', 'n', 'p', 'q', 'q', 'r', 's', 't', 'v', 'v', 'v', 'v', 'w', 'x', 'x', 'x', 'x', 'z', 'z']
['b', 'c', 'd', 'f', 'g', 'j', 'k', 'l', 'm', 'm', 'n', 'p', 'q', 'q', 'r', 's', 'v', 'v', 'v', 'v', 'w', 'x', 'x', 'x', 'x', 'z']
['b', 'c', 'd', 'f', 'g', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'q', 'r', 'v', 'v', 'v', 'v', 'w', 'x', 'x', 'x', 'x', 'z']
['b', 'c', 'd', 'f', 'g', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'q', 'r', 'v', 'v', 'w', 'x', 'x', 'z']
['c', 'f', 'g', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'q', 'r', 'v', 'v', 'w', 'x', 'x', 'z']
['c', 'f', 'g', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'q', 'v', 'v', 'w', 'x', 'z']
['c', 'f', 'g', 'k', 'l', 'm', 'n', 'p', 'q', 'q', 'v', 'v', 'w', 'z']
['c', 'f', 'k', 'l', 'm', 'n', 'p', 'q', 'q', 'v', 'w', 'z']
['c', 'f', 'k', 'l', 'm', 'n', 'p', 'q', 'q', 'v']
['c', 'f', 'k', 'n', 'p', 'q', 'q', 'v']
['c', 'n', 'p', 'q', 'q', 'v']
['n', 'p', 'q', 'q']
['p', 'q']
[] <--- Causes the error
If you need 20 times a random of 4 characters with this pattern: consonant+vowel+consonant+vowel, this is the solution:
import random
vowels = 'aeiouy'
consonants =['b','c','d','f','g','h','j','k','l','m','m','n','p','q','q','r','s','t','v','v','v','v','w','x','x','x','x','z','z','z','z']
for num in range(20):
consonantsCopy = consonants.copy()
firstConsonant = random.choice(consonantsCopy)
consonantsCopy.remove(firstConsonant)
secondConsonant = random.choice(consonantsCopy)
consonantsCopy.remove(secondConsonant)
tempString = firstConsonant + random.choice(vowels) + secondConsonant + random.choice(vowels)
print(tempString)
Output:
sufa
quzu
zela
bixo
beju
mafy
vybe
zumo
kozo
I think you have a lot of for in your code.
What's string you need to expected at the end?
If you need a string long 20 chars, with unique consonant taken from your array, you need write like this:
import random
word = "";
vowels = 'aeiouy'
consonants =['b','c','d','f','g','h','j','k','l','m','m','n','p','q','q','r','s','t','v','v','v','v','w','x','x','x','x','z','z','z','z']
for num in range(20):
i = random.choice(consonants)
consonants.remove(i)
word += random.choice(vowels)
word += i
print (word)
In this case the random string is composed like this pattern: (vowel+consonant)*20
Output:
utivecojyvuluzavomuxydimisokeziwanohubyx
Related
normal = ['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']
modified = ['s', 'n', 'v', 'f', 'r', 'g', 'h', 'j', 'o', 'k', 'l', 'a',
'z', 'm', 'p', 'q', 'w', 't', 'd', 'y', 'i', 'b', 'e', 'c', 'u', 'x']
word = input()
for char in word:
if char in normal:
char.replace(char, modified)
This is what i have so far,
I want to be able to type in a sentence and it will output the sentence with the modified alphabets
one of the way to map the normal list character with the modified list character and replace them in the sentence.
normal = ['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']
modified = ['s', 'n', 'v', 'f', 'r', 'g', 'h', 'j', 'o', 'k', 'l', 'a',
'z', 'm', 'p', 'q', 'w', 't', 'd', 'y', 'i', 'b', 'e', 'c', 'u', 'x']
mapper = {}
for a, b in zip(normal, modified):
mapper[b] = a
word ="this is user commented word"
# new_word = ' '.join(map(lambda x: ''.join(mapper[i] for i in x), word.split()))
new_word = []
for i in word.split():
tmp = []
for j in i:
tmp.append(mapper[j])
new_word.append(''.join(tmp))
new_sentence = ' '.join(new_word)
print(new_sentence)
output
rgua ua yawe xinnwbrws qies
normal = 'abcdefghijklmnopqrstuvwxyz'
modified = 'lvxswdfguhjknbiopearycqztm'
word = input()
convert = str.maketrans(normal, modified)
print(word.translate(convert))
This question already has answers here:
Iterate an iterator by chunks (of n) in Python?
(14 answers)
Closed 2 years ago.
Here's a list that I have,
data = (i for i in list("abcdefghijklmnopqrstuvwxyzabcedefghijklmnopqrstuvwxyz"))
Here data is a generator and I want to iterate over it and prepared batches of 12 equal datapoints, if it is less than 12 in last batch I need it too, but below code is not working,
subsets = []
subset = []
for en, i in enumerate(data):
if en % 12 == 0 and en > 0:
subsets.append(subset)
subset = []
else:
subset.append(i)
print(subsets)
[['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'],
['n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x'],
['z', 'a', 'b', 'c', 'e', 'd', 'e', 'f', 'g', 'h', 'i'],
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u']]
But my code is not working properly because the first nested list has 12 values but rest of it have 11 values and it missed out last few values which are less than 12 in the last batch
Expected Output:
[['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', 'e', 'd', 'e', 'f', 'g', 'h', 'i'],
['j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u'],
['v', 'w', 'x', 'y', 'z']]
Two changes, you need to start iterating from 1 and append in the sublist before emptying it:
data = (i for i in list("abcdefghijklmnopqrstuvwxyzabcedefghijklmnopqrstuvwxyz"))
subsets = []
subset = []
# start counting from index '1'
for en, i in enumerate(data, 1):
if en % 12 == 0 and en > 0:
# append the current element before emptying 'subset'
subset.append(i)
subsets.append(subset)
subset = []
else:
subset.append(i)
# append the left-over sublist/subset to your main list as well
subsets.append(subset)
for i in subsets:
print(i)
gives
['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', 'e', 'd', 'e', 'f', 'g', 'h', 'i']
['j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u']
['v', 'w', 'x', 'y', 'z']
Alternative solution is using buit-in itertools.islice. You can check to see which approach is faster or more convenient. Kr.
import itertools
def gen_sublist(your_iter, size):
while True:
part = tuple(itertools.islice(your_iter, size))
if not part:
break
yield part
data = (i for i in list("abcdefghijklmnopqrstuvwxyzabcedefghijklmnopqrstuvwxyz"))
for c in gen_sublist(data, size=12):
print(c)
which returns:
('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', 'e', 'd', 'e', 'f', 'g', 'h', 'i')
('j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u')
('v', 'w', 'x', 'y', 'z')
A different approach which does not use modulo or enumeration (just another option since other answers already correct your approach):
In [1]: subsets = []
In [2]: data = (i for i in list("abcdefghijklmnopqrstuvwxyzabcedefghijklmnopqrstuvwxyz"))
In [3]:
...: while True:
...: try:
...: x = []
...: for i in range(12):
...: x.append(next(data))
...: subsets.append(x)
...: except: # Catch StopIteration Exception when generator runs out of values
...: subsets.append(x)
...: break
...:
Outputs:
In [4]: subsets
Out[4]:
[['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', 'e', 'd', 'e', 'f', 'g', 'h', 'i'],
['j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u'],
['v', 'w', 'x', 'y', 'z']]
So I'm trying to add a new list to a 2d array inside a for loop, by taking the last letter of the list and putting it to the start, and adding this new list to the 2d array.
But although the new lists work, when I append to the 2d array, it just appends the normal alphabet, not my new one.
Can anybody see what I'm doing wrong?
def tabulaRecta():
import string
alpha = list(string.ascii_uppercase)
l2d = []
l2d.append(alpha)
for i in range(26):
temp = alpha[len(alpha)-1]
alpha.pop()
alpha.insert(0,temp)
l2d.append(alpha)
print(l2d)
tabulaRecta()
The program is supposed to output something like this, but all of these within a list:
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
['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']
instead it prints this 26 times in a list:
['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']
Instead of saying print(...) you can append it to a list and print it when the loop is finished
s = list(string.ascii_uppercase)
>>> for i in range(len(s),-1,-1):
... print(s[i:]+s[:i])
['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']
['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']
['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']
['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']
.
.
.
.
A 2d array is an array of pointers to arrays, and in your code each one points to the same list, that you are modifying 26 times eventually coming back to the original one.
You have to make a copy of the list each time you modify it.
For example, add a alpha = list(alpha) at the beginning of the for loop:
def tabulaRecta():
import string
alpha = list(string.ascii_uppercase)
l2d = []
l2d.append(alpha)
for i in range(26):
alpha = list(alpha)
temp = alpha[len(alpha) - 1]
alpha.pop()
alpha.insert(0, temp)
l2d.append(alpha)
print(l2d)
tabulaRecta()
You could also just do this:
import string
def tabula_rect():
alpha = list(string.ascii_uppercase)
l2d = []
l2d.append(alpha)
for i in range(len(alpha)):
temp = alpha[len(alpha)-1]
alpha = alpha[:-1]
alpha.insert(0, temp)
l2d.append(alpha)
return l2d
RNA_codon_dictonary = {
'UUU': 'F', 'CUU': 'L', 'AUU': 'I', 'GUU': 'V',
'UUC': 'F', 'CUC': 'L', 'AUC': 'I', 'GUC': 'V',
'UUA': 'L', 'CUA': 'L', 'AUA': 'I', 'GUA': 'V',
'UUG': 'L', 'CUG': 'L', 'AUG': 'M', 'GUG': 'V',
'UCU': 'S', 'CCU': 'P', 'ACU': 'T', 'GCU': 'A',
'UCC': 'S', 'CCC': 'P', 'ACC': 'T', 'GCC': 'A',
'UCA': 'S', 'CCA': 'P', 'ACA': 'T', 'GCA': 'A',
'UCG': 'S', 'CCG': 'P', 'ACG': 'T', 'GCG': 'A',
'UAU': 'Y', 'CAU': 'H', 'AAU': 'N', 'GAU': 'D',
'UAC': 'Y', 'CAC': 'H', 'AAC': 'N', 'GAC': 'D',
'UAA': 'Stop', 'CAA': 'Q', 'AAA': 'K', 'GAA': 'E',
'UAG': 'Stop', 'CAG': 'Q', 'AAG': 'K', 'GAG': 'E',
'UGU': 'C', 'CGU': 'R', 'AGU': 'S', 'GGU': 'G',
'UGC': 'C', 'CGC': 'R', 'AGC': 'S', 'GGC': 'G',
'UGA': 'Stop', 'CGA': 'R', 'AGA': 'R', 'GGA': 'G',
'UGG': 'W', 'CGG': 'R', 'AGG': 'R', 'GGG': 'G'
}
def RNA_to_Protien(mRNA_seq):
codon = []
if codon in RNA_codon_dictonary:
# return the aminoacid by looking up in the dictionary:
return RNA_codon_dictonary[codon]
else:
# return '' if we could not translate the codon:
return '?'
if __name__ == "__main__":
mRNA_seq = "UCAAUGUAACGCGCUACCCGGAGCUCUGGGCCCAAAUUUCAUCCACU"
print (RNA_to_Protien(mRNA_seq))
You are checking to see if the empty list is a key in your dictionary. There are two problems with that:
1) The answer will always be no, since your dictionary doesn't have any empty keys, and
2) That operation isn't even allowed, since lists are never allowed to be keys of a dict.
Based on your comment, the following code might be what you are looking for. It breaks the sequence in non-overlapping substrings of length 3, looks up each substring in the dict, and returns all of the results.
def RNA_to_Protien(mRNA_seq):
return [
RNA_codon_dictonary.get(mRNA_seq[i:i+3], '?')
for i in range(0, len(mRNA_seq), 3)
]
In your example sequence, this returns:
['S', 'M', 'Stop', 'R', 'A', 'T', 'R', 'S', 'S', 'G', 'P', 'K', 'F', 'H', 'P', '?']
Or, if you would rather lookup overlapping sequences, try this:
def RNA_to_Protien(mRNA_seq):
return [
RNA_codon_dictonary.get(mRNA_seq[i:i+3], '?')
for i in range(0, len(mRNA_seq)-2, 1)
]
It yields this result:
['S', 'Q', 'N', 'M', 'C', 'V', 'Stop', 'N', 'T', 'R', 'A', 'R', 'A', 'L', 'Y', 'T', 'P', 'P', 'R', 'G', 'E', 'S', 'A', 'L', 'S', 'L', 'W', 'G', 'G', 'A', 'P', 'P', 'Q', 'K', 'N', 'I', 'F', 'F', 'S', 'H', 'I', 'S', 'P', 'H', 'T']
And, based on your request to return a single string instead of a list of strings:
def RNA_to_Protien(mRNA_seq):
return ''.join(
RNA_codon_dictonary.get(mRNA_seq[i:i+3], '?')
for i in range(0, len(mRNA_seq), 3)
)
This yields:
'SMStopRATRSSGPKFHP?'
This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 8 years ago.
My list is ,
['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']
want to print every n elements of this list. Means in each iteration want to print N*elements from that list.
I tried ,
If N is 15.
>>> a=list(string.ascii_lowercase)
>>> for i in range(len(a)-1):
print a[i:i+15]
It prints
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o']
['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q']
['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r']
['e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's']
['f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't']
['g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u']
['h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w']
['j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y']
['l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['s', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['t', 'u', 'v', 'w', 'x', 'y', 'z']
['u', 'v', 'w', 'x', 'y', 'z']
['v', 'w', 'x', 'y', 'z']
['w', 'x', 'y', 'z']
['x', 'y', 'z']
['y', 'z']
What I want is ,
if N=15
['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']
If N=5 ,
['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']
How to do this ?
Just use a step on the range:
for i in range(0, len(a), 15):
print a[i:i+15]
Also note that I changed your len(a)-1 to len(a). Ranges in Python are half-open, meaning you give it the first value, and one past the last value. So, to count the first 26 numbers, from 0 to 25, you use range(26), not range(25).
There may be better ways to write this; for example, with a grouper or chunker function like the one in the itertools recipes:
for group in grouper(a, 15, fillvalue=None):
print filter(None, group)
But the step is the smallest change from what you have.