Trying to append modified lists within a for loop - python
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
Related
How to prepare batches of data from a list of values? [duplicate]
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']]
I just want to get same index letter for each element on my list
For ex: a = "pandaxngeqrymtso-ezmlaesowxaqbujl-noilktxreecytrql-gskaboofsfoxdtei-utsmakotufodhlrd-iroachimpanzeesa-nintrwflyrkhcdum-jcecahkktiklsvhr-mhvsbaykagodwgca-koalatcwlkfmrwbb-jsrrfdolphinuyt" a = a.split("-") mylist = [] word = "" while i < len(a[0]): #16 for elem in a: word+= elem[i] mylist.append(kelime) i += 1 word = "" I just want a list which contains "penguinjmhkj, azostrichos..." But I get an Index error. What can I do?
You can try this: >>> word = [''.join(letters) for letters in zip(*(list(word) for word in a.split('-')))] >>> word ['penguinjmkj', 'azostrichos', 'nmiksonevar', 'dllamatcslr', 'aakbacrabaf', 'xetokhwhatd', 'nsxooifkyco', 'gorftmlkkwl', 'ewesupytalp', 'qxeffarigkh', 'racoonkkofi', 'yqyxdzhldmn', 'mbtdhecswru', 'turtledvgwy', 'sjqersuhcbt'] Explanation: You can understand what it is doing if you print the individual parts, for e.g.: >>> print(*(list(word) for word in a.split('-')) ['p', 'a', 'n', 'd', 'a', 'x', 'n', 'g', 'e', 'q', 'r', 'y', 'm', 't', 's', 'o'] ['e', 'z', 'm', 'l', 'a', 'e', 's', 'o', 'w', 'x', 'a', 'q', 'b', 'u', 'j', 'l'] ['n', 'o', 'i', 'l', 'k', 't', 'x', 'r', 'e', 'e', 'c', 'y', 't', 'r', 'q', 'l'] ['g', 's', 'k', 'a', 'b', 'o', 'o', 'f', 's', 'f', 'o', 'x', 'd', 't', 'e', 'i'] ['u', 't', 's', 'm', 'a', 'k', 'o', 't', 'u', 'f', 'o', 'd', 'h', 'l', 'r', 'd'] ['i', 'r', 'o', 'a', 'c', 'h', 'i', 'm', 'p', 'a', 'n', 'z', 'e', 'e', 's', 'a'] ['n', 'i', 'n', 't', 'r', 'w', 'f', 'l', 'y', 'r', 'k', 'h', 'c', 'd', 'u', 'm'] ['j', 'c', 'e', 'c', 'a', 'h', 'k', 'k', 't', 'i', 'k', 'l', 's', 'v', 'h', 'r'] ['m', 'h', 'v', 's', 'b', 'a', 'y', 'k', 'a', 'g', 'o', 'd', 'w', 'g', 'c', 'a'] ['k', 'o', 'a', 'l', 'a', 't', 'c', 'w', 'l', 'k', 'f', 'm', 'r', 'w', 'b', 'b'] ['j', 's', 'r', 'r', 'f', 'd', 'o', 'l', 'p', 'h', 'i', 'n', 'u', 'y', 't'] So it breaks up all the individual words delimited by - into characters. Then zip does this: >>> print(zip(*(list(word) for word in a.split('-')))) ('p', 'e', 'n', 'g', 'u', 'i', 'n', 'j', 'm', 'k', 'j') ('a', 'z', 'o', 's', 't', 'r', 'i', 'c', 'h', 'o', 's') ('n', 'm', 'i', 'k', 's', 'o', 'n', 'e', 'v', 'a', 'r') ('d', 'l', 'l', 'a', 'm', 'a', 't', 'c', 's', 'l', 'r') ('a', 'a', 'k', 'b', 'a', 'c', 'r', 'a', 'b', 'a', 'f') ('x', 'e', 't', 'o', 'k', 'h', 'w', 'h', 'a', 't', 'd') ('n', 's', 'x', 'o', 'o', 'i', 'f', 'k', 'y', 'c', 'o') ('g', 'o', 'r', 'f', 't', 'm', 'l', 'k', 'k', 'w', 'l') ('e', 'w', 'e', 's', 'u', 'p', 'y', 't', 'a', 'l', 'p') ('q', 'x', 'e', 'f', 'f', 'a', 'r', 'i', 'g', 'k', 'h') ('r', 'a', 'c', 'o', 'o', 'n', 'k', 'k', 'o', 'f', 'i') ('y', 'q', 'y', 'x', 'd', 'z', 'h', 'l', 'd', 'm', 'n') ('m', 'b', 't', 'd', 'h', 'e', 'c', 's', 'w', 'r', 'u') ('t', 'u', 'r', 't', 'l', 'e', 'd', 'v', 'g', 'w', 'y') ('s', 'j', 'q', 'e', 'r', 's', 'u', 'h', 'c', 'b', 't') So it takes all the corresponding characters from each group, each of the tuples get passed as letters in each iteration in the main code. Then you join each tuple, for e.g. in first iteration: >>> ''.join(('p', 'e', 'n', 'g', 'u', 'i', 'n', 'j', 'm', 'k', 'j')) 'penguinjmkj' [<item after some operation> for <each item> in <item_list>] this structure is called list comprehension. * is used for iterable unpacking.
it took a while but i cracked it; what you just need to do is to handle the Index Error: String out of range. if you count the words they are over 16 while the array items after splitting are just over 11. to cut long story short; Handle the exception with a try_except where you are appending the letters at: word+= elem[i] here is my code and how i solved it using try_catch a = "pandaxngeqrymtso-ezmlaesowxaqbujl-noilktxreecytrql-gskaboofsfoxdtei-utsmakotufodhlrd-iroachimpanzeesa-nintrwflyrkhcdum-jcecahkktiklsvhr-mhvsbaykagodwgca-koalatcwlkfmrwbb-jsrrfdolphinuyt" newArr = a.split('-') newWord = [] i = 0 mylist = [] while i < len(newArr[0]): word = "" for item in newArr: try: word += item[i] except: break i += 1 mylist.append(word) print(mylist) I used a try_except to handle the Index Error when appending the letter, then break when ever the 'i' used for the while loop is greater than the newArr length in the for loop. try for your self!
Similar to the code from Sayandip, but it would have been more readable for me in the past: mylist =[] for element in zip(*a.split('-')): mylist.append(''.join(element)) print(mylist) I get ['penguinjmkj', 'azostrichos', 'nmiksonevar', 'dllamatcslr', 'aakbacrabaf', 'xetokhwhatd', 'nsxooifkyco', 'gorftmlkkwl', 'ewesupytalp', 'qxeffarigkh', 'racoonkkofi', 'yqyxdzhldmn', 'mbtdhecswru', 'turtledvgwy', 'sjqersuhcbt'] I am moving the splitting in the for loop, and using the * construct to pass the whole list resulting from splitting, see usage here and here.
Remove a '\n' list item from a list of a reading file
I have a similar problem. My code so far is: file='/Users/Giannis/Desktop/Python Assegments/Week 6/boardlist1.txt' boardlist = [] file = open(file, 'r') line = file.readlines() wordstring = '' for i in range(0,len(line)): final_list = [] raw = list(line[i]) boardlist.append(raw) print(boardlist) file.close() The file you see is a txt which is: EFJAJCOWSS SDGKSRFDFF ASRJDUSKLK HEANDNDJWA ANSDNCNEOP PMSNFHHEJE JEPQLYNXDL My print results are: [['E', 'F', 'J', 'A', 'J', 'C', 'O', 'W', 'S', 'S', '\n'], ['S', 'D', 'G', 'K', 'S', 'R', 'F', 'D', 'F', 'F', '\n'], ['A', 'S', 'R', 'J', 'D', 'U', 'S', 'K', 'L', 'K', '\n'], ['H', 'E', 'A', 'N', 'D', 'N', 'D', 'J', 'W', 'A', '\n'], ['A', 'N', 'S', 'D', 'N', 'C', 'N', 'E', 'O', 'P', '\n'], ['P', 'M', 'S', 'N', 'F', 'H', 'H', 'E', 'J', 'E', '\n'], ['J', 'E', 'P', 'Q', 'L', 'Y', 'N', 'X', 'D', 'L']] And I want to remove every \n character in it. How can I do that with this code?
Use strip(), here is the shortest version of your code : file='/Users/Giannis/Desktop/Python Assegments/Week 6/boardlist1.txt' with open(file, 'r') as f: print([list(i) for i in [i.strip() for i in f]]) [['E', 'F', 'J', 'A', 'J', 'C', 'O', 'W', 'S', 'S'], ['S', 'D', 'G', 'K', 'S', 'R', 'F', 'D', 'F', 'F'], ['A', 'S', 'R', 'J', 'D', 'U', 'S', 'K', 'L', 'K'], ['H', 'E', 'A', 'N', 'D', 'N', 'D', 'J', 'W', 'A'], ['A', 'N', 'S', 'D', 'N', 'C', 'N', 'E', 'O', 'P'], ['P', 'M', 'S', 'N', 'F', 'H', 'H', 'E', 'J', 'E'], ['J', 'E', 'P', 'Q', 'L', 'Y', 'N', 'X', 'D', 'L']] Note : You don't need to use readlines(), just iterate the file object.
Why do I get the error TypeError: unhashable type: 'list'?
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?'
Print every n elements from list python [duplicate]
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.