Saturday, October 5, 2013

Useful Python tips (in progress)

-tt

sorted( x, reverse=T, key=len)

'hello:world'.split(":")

a = [(1,'b'), (2,'b'),(1,'a')]
sorted(a)

'aaa bbb ccc'.split() #default split is white spaces

(x,y) = (2, 3)

d = {}
d['a'] = 'alpha'
d['o'] = 'omega'
 'o' in d

genetic_code = {
    'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M',
    'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T',
    'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K',
    'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',
    'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L',
    'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P',
    'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q',
    'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R',
    'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V',
    'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A',
    'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E',
    'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G',
    'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S',
    'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L',
    'TAC':'Y', 'TAT':'Y', 'TAA':'_', 'TAG':'_',
    'TGC':'C', 'TGT':'C', 'TGA':'_', 'TGG':'W'}

f = open(filename, 'rV')
lines = f.readlines() #entire file to one list
OneString = f.read() #entire file to one string.
f.close()

import re
match_object = re.search(pattern, text)
match_object.group()


if match:

r'c\.'' #raw pattern
r':\w\w\w' #match 3 characters
r'\d+\s+\d+'
r'\w[\w.]*@[\w.]+' #for email addresses

re.findall()





No comments:

Post a Comment