[英]How to append a dictionary to another dictionary string key in Python3?
[英]How to append characters to a string being used as a python dictionary key (when there are multiple entries related to that string)?
我从HMMER生成的输出文件中提取序列坐标(找到DNA序列,在基因组装配文件中匹配查询)。
我创建了一个python字典,其中键是源序列名称(字符串),值是包含目标序列的起始和结束坐标的列表。 然而,HMMER经常在单个源序列(重叠群/染色体)上发现多个匹配。
这意味着当我添加到字典中时,如果我遇到重叠群上的多个匹配项,则每个匹配项都会被覆盖。
例如,HMMER发现以下匹配:
名称开始结束
4415 16723 17556
127 1290 1145
1263 34900 37834
4415 2073 3899
4415 4580 6004
但这会产生以下字典(我希望每个匹配单独输入):
{'127':['1290','1145'],'1263':['34900','37834'],'4415':['4580','6004']}
如何在密钥上附加一个字母,以便后续匹配是唯一的,不会覆盖以前的匹配,即4415,4415a,4415b等等?
matches = {}
for each line of HMMER file:
split the line
make a list of fields 4 & 5 (the coordinates)
# at this stage I need a way of checking whether the key (sequenceName)
# is already in the dictionary (easy), and if it is, appending a letter
# to sequenceName to make it unique
matches[sequenceName] = list
在相同的情况下,创建不同的键不是一种正确的方法,而是可以使用列表作为值并保留其中的坐标,以用于重复键。 您可以使用collections.defaultdict()
实现此目标:
>>> coords = [['4415', '16723', '17556'], ['127', '1290', '1145'], ['1263', '34900', '37834'], ['4415', '2073', '3899'], ['4415', '4580', '6004']]
>>> from collections import defaultdict
>>>
>>> d = defaultdict(list)
>>>
>>> for i, j, k in coords:
... d[i].append((j, k))
...
>>> d
defaultdict(<type 'list'>, {'1263': [('34900', '37834')], '4415': [('16723', '17556'), ('2073', '3899'), ('4580', '6004')], '127': [('1290', '1145')]})
此外,在键的末尾添加字符的想法并不是最佳的,因为您需要始终拥有键的数量而您不知道这个数字,因此您必须生成新的后缀。
但是,如果您只使用键的计数,则可以通过保留Counter()
对象中的键并在键的尾随处添加计数来创建不同的键:
>>> from collections import Counter
>>> d = {}
>>> c = Counter()
>>> for i, j, k in coords:
... c.update((i,))
... d["{}_{}".format(i, c[i])] = (j, k)
...
>>> d
{'4415_1': ('16723', '17556'), '4415_3': ('4580', '6004'), '4415_2': ('2073', '3899'), '127_1': ('1290', '1145'), '1263_1': ('34900', '37834')}
你可以这样做:
matches = {'127': ['1290', '1145'], '1263': ['34900', '37834'], '4415': ['4580', '6004']}
# sample key_name
key_name = '4415'
if key_name in matches.keys():
for i in xrange(1,26):
if key_name+chr(ord('a') + i) not in matches.keys():
matches[key_name+chr(ord('a') + i)] = #your value
这会将你的key_names增加为4415a,4415b ......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.