gpt4 book ai didi

python - 在 python 中向嵌套字典添加新值

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:54 28 4
gpt4 key购买 nike

我将文件中的几条不同信息分类到列表中,并希望将它们添加到嵌套字典中。

输入

exon    65419   65433   gene_id "ENSG00000186092"; transcript_id "ENST00000641515"; exon_number 1
exon 65520 65573 gene_id "ENSG00000186092"; transcript_id "ENST00000641515"; exon_number 2
CDS 65565 65573 gene_id "ENSG00000186092"; transcript_id "ENST00000641515"; exon_number 2
exon 69037 71585 gene_id "ENSG00000186092"; transcript_id "ENST00000641515"; exon_number 3
CDS 69037 70005 gene_id "ENSG00000186092"; transcript_id "ENST00000641515"; exon_number 3
exon 69055 70108 gene_id "ENSG00000186092"; transcript_id "ENST00000335137"; exon_number 1
CDS 69091 70005 gene_id "ENSG00000186092"; transcript_id "ENST00000335137"; exon_number 1

期望输出

{'ENSG00000186092': {'ENST00000335137': {'exon_start': ['69055'],
'exon_stop': ['70108']},
'ENST00000641515': {'exon_start': ['65419', '65520', '69037'],
'exon_stop': ['65433', '65573', '71585']}}}

当前尝试

class Vividict(dict):
def __missing__(self, key):
value = self[key] = type(self)() # retain local pointer to value
return value # faster to return than dict lookup

all_info = Vividict()

for line in infile:
if not line.startswith("##"):
item = line.rstrip().split("\t")
info = item[8].split(";")
geneID = info[0].split(" ")[1]
geneID = geneID.strip('\"')
gtf_t_id = info[1].split(" ")[2]
gtf_t_id = gtf_t_id.strip('\"')
if item[2] == "exon":
num = info[6].split(" ")[2]
start = item[3]
stop = item[4]
if start in all_info[geneID][gtf_t_id]["exon_start"]:
all_info[geneID][gtf_t_id]["exon_start"].append(start)
else:
all_info[geneID][gtf_t_id]["exon_start"] = [start]
if stop in all_info[geneID][gtf_t_id]["exon_stop"]:
all_info[geneID][gtf_t_id]["exon_stop"].append(stop)
else:
all_info[geneID][gtf_t_id]["exon_stop"] = [stop]

当前结果

{'ENSG00000186092': {'ENST00000335137': {'exon_start': ['69055'],
'exon_stop': ['70108']},
'ENST00000641515': {'exon_start': ['69037'],
'exon_stop': ['71585']}}}

最佳答案

你的代码工作正常,但是当开始/结束值是新的时它会继续初始化并且不存在于该列表中,它会覆盖它并转到其他条件并进行新列表包含 1 个元素

class Vividict(dict):
def __missing__(self, key):
value = self[key] = type(self)() # retain local pointer to value
return value # faster to return than dict lookup

all_info = Vividict()

for line in infile:
if not line.startswith("##"):
item = line.rstrip().split("\t")
info = item[8].split(";")
geneID = info[0].split(" ")[1]
geneID = geneID.strip('\"')
gtf_t_id = info[1].split(" ")[2]
gtf_t_id = gtf_t_id.strip('\"')
if item[2] == "exon":
num = info[6].split(" ")[2]
start = item[3]
stop = item[4]
try:
if all_info[geneID][gtf_t_id]["exon_start"]:
all_info[geneID][gtf_t_id]["exon_start"].append(start)

except KeyError:
all_info[geneID][gtf_t_id]["exon_start"] = [start]

try:

if all_info[geneID][gtf_t_id]["exon_stop"]:
all_info[geneID][gtf_t_id]["exon_stop"].append(stop)
except KeyError:
all_info[geneID][gtf_t_id]["exon_stop"] = [stop]

关于python - 在 python 中向嵌套字典添加新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56083115/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com