1、函数实现读文件操作和对文件内容进行处理的操作
def read_seq(inputfile):
"""Reads and returns the input sequence with special characters removed."""
# your code here
file = open(inputfile,"r")
seq = file.read()
seq = seq.replace("\n","")
seq = seq.replace("\r","")
return seq
2、利用python中的字典实现翻译函数:
即:将含有核苷酸序列的字符串翻译成含有相应氨基酸序列的字符串。
使用表格字典将核苷酸翻译成三联体;每个氨基酸用长度为1的字符串编码。
#step 1:检查序列的长度是否可被3整除
#step 2:循环表中的每个3个字母的字符串并存储结果
#step 3:继续循环,直到到达序列结束
def translate(seq):
"""Translate a string containing a nucleotide sequence
into a string containing the corresponding sequence of
amino acids. Nucleotides are translated in triplets using
the table dictionary; each amino acid is encoded with
a string of length 1."""
table = {
'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'
}
# check that the sequence length is divisible by 3
# loop over the sequence, extract a single codon
# loop up the codon and store the result
protein = ""
# your code here
for i in range(0,len(seq),3):
protein += table[seq[i:i+3]]
return protein
3、实现encode_dict(encryption_key)函数
#参数为encryption_key;
#该函数创建一个名为encoding的字典
#键是字母表中的字符
#values是0-26之间的数字,移位整数encryption_key。
# 例如:
#键'a'应具有值encryption_key,键'b'应具有值encryption_key + 1,依此类推。
#如果此添加的任何结果小于0或大于26,则可以使用结果%27确保结果保持在0-26之内。
def encode_dict(encryption_key):
# your code is here
alphabet = string.ascii_lowercase + " "
encoding = {}
j = encryption_key%27
for item in alphabet:
encoding[item] = j%27
j += 1
return encoding
4、实现caesar(message,encryption_key)函数,即凯撒密码加密函数
使用两个参数message和encryption_key编写一个名为caesar的函数,用Caesar密码对消息进行编码。
#使用您的代码查找消息中每个字母的编码值。
#将这些值用作字典字母中的键,以确定消息中每个字母的编码字母。
#您的函数应返回由这些编码字母组成的字符串。
def caesar(message,encryption_key):
# return the encoded message as a single string!
encoded_message = ''
# use the function in Step 2 to get the encoding dictionary
encoding = encode_dict(encryption_key)
# your code is here
# for each letter in message, get the encoded letter
alphabet = string.ascii_lowercase + " "
letters = {}
j = 0
for item in alphabet:
letters[j] = item
j += 1
for item in message:
encoded_message += letters[encoding[item]]
return encoded_message
结果如下:
(1)、加密:
message = "hi my name is caesar"
encryption_key = 3
encoded_message = caesar(message,encryption_key)
print(encoded_message)
(2)、解密
decoded_message = caesar(encoded_message,-3)
print(decoded_message)
总结:
我发现根据键求字典中的值比较容易,但是其实也可以根据值来去求得键,有以下3种方法:
1.print([k for k,v in dict.items() if v==200])
2.b=list(dict.keys())[list(dict.values()).index(200)]
将字典的值变列表,找目标下标,将键变成列表,根据刚才的下标求得值
3.new_dict={v:k for k,v in dict.items()}
print(new_dict)
print(new_dict[200])