我有一个字典看起来像这样,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”,但我的代码失败了。任何人都可以帮我修改我的代码吗?
答案 0 :(得分:2)
换行符不是你的问题,虽然它们很混乱。您正尝试根据字符串索引dict.values()
返回的view。这不仅不是你想要的,而且它也首先打败了使用字典的整个目的。视图是可迭代的,而不是像dicts那样的映射。只需在正常的方式中查找字典中的值:
return (self.dna_seq, self.__fastqdict[self.dna_seq + "\n"])
至于换行符,为什么不首先在构建字典时将它们删除?
答案 1 :(得分:0)
要修改字典,您只需执行以下操作:
myNewDict = {}
for var in myDict:
myNewDict[var.strip()] = myDict[var].strip()
答案 2 :(得分:0)
您可以从字典的键和值中删除那些讨厌的换行符(假设您的字典存储在名为dna
的变量中):
dna = {k.rstrip(): v.rstrip() for k, v in dna.iteritems()}