2014-02-12 54 views
0

「\ n」我有一本字典是這樣的,在DNA是關鍵,質量值是值:消除字典

{'TTTGTTCTTTTTGTAATGGGGCCAGATGTCACTCATTCCACATGTAGTATCCAGATTGAAATGAAATGAGGTAGAACTGACCCAGGCTGGACAAGGAAGG\n': 
    'eeeecdddddaaa`]eceeeddY\\cQ]V[F\\\\TZT_b^[^]Z_Z]ac_ccd^\\dcbc\\TaYcbTTZSb]Y]X_bZ\\a^^\\S[T\\aaacccBBBBBBBBBB\n', 
'ACTTATATTATGTTGACACTCAAAAATTTCAGAATTTGGAGTATTTTGAATTTCAGATTTTCTGATTAGGGATGTACCTGTACTTTTTTTTTTTTTTTTT\n': 
    'dddddd\\cdddcdddcYdddd`d`dcd^dccdT`cddddddd^dddddddddd^ddadddadcd\\cda`Y`Y`b`````adcddd`ddd_dddadW`db_\n', 
'CTGCCAGCACGCTGTCACCTCTCAATAACAGTGAGTGTAATGGCCATACTCTTGATTTGGTTTTTGCCTTATGAATCAGTGGCTAAAAATATTATTTAAT\n': 
    'deeee`bbcddddad\\bbbbeee\\ecYZcc^dd^ddd\\\\`]``L`ccabaVJ`MZ^aaYMbbb__PYWY]RWNUUab`Y`BBBBBBBBBBBBBBBBBBBB\n'} 

我想編寫一個函數,這樣如果我查詢的DNA序列,它返回該DNA序列的元組和相應的質量值

我寫了下面的功能,但它給了我,說:list indices must be integers, not str

def query_sequence_id(self, dna_seq=''): 
    """Overrides the query_sequence_id so that it optionally returns both the sequence and the quality values. 
    If DNA sequence does not exist in the class, return a string error message""" 
    list_dna = [] 
    for t in self.__fastqdict.keys(): 
     list_dna.append(t.rstrip('\n')) 
    self.dna_seq = dna_seq 
    if self.dna_seq in list_dna: 
     return (self.dna_seq,self.__fastqdict.values()[self.dna_seq + "\n"]) 
    else: 
     return "This DNA sequence does not exist" 

所以我想一個錯誤信息像如果我打印

query_sequence_id("TTTGTTCTTTTTGTAATGGGGCCAGATGTCACTCATTCCACATGTAGTATCCAGATTGAAATGAAATGAGGTAGAACTGACCCAGGCTGGACAAGGAAGG"), 

我會得到

('TTTGTTCTTTTTGTAATGGGGCCAGATGTCACTCATTCCACATGTAGTATCCAGATTGAAATGAAATGAGGTAGAACTGACCCAGGCTGGACAAGGAAGG', 
'eeeecdddddaaa`]eceeeddY\\cQ]V[F\\\\TZT_b^[^]Z_Z]ac_ccd^\\dcbc\\TaYcbTTZSb]Y]X_bZ\\a^^\\S[T\\aaacccBBBBBBBBBB') 

我想擺脫「\ n」表示鍵和值的,但我的代碼失敗。任何人都可以幫我修復我的代碼?

+1

爲什麼不去掉插入數據時不必要的換行符,然後根本就沒有它們呢? – mikebabcock

回答

2

換行符不是你的問題,雖然它們很混亂。您正試圖根據字符串爲由dict.values()返回的view編制索引。這不僅不是你想要的,但它也擺脫了首先使用字典的全部目的。視圖是可迭代的,而不是像字典那樣的映射。只是仰望值在字典中,以正常的方式:

return (self.dna_seq, self.__fastqdict[self.dna_seq + "\n"]) 

至於新行,爲什麼不你建立在首位字典時只帶他們出去?

+0

謝謝。你的修改是完美的! – neymar

0

要修改詞典中,你可以做以下幾點:

myNewDict = {} 
for var in myDict: 
    myNewDict[var.strip()] = myDict[var].strip() 
0

你可以從你的字典的鍵和這樣的數值去掉這些討厭的換行符(假設你的字典存儲在一個名爲dna變量):

dna = {k.rstrip(): v.rstrip() for k, v in dna.iteritems()}