从.txt文件创建多个嵌套词典 [英] Creating multiple nested dictionaries from .txt file
本文介绍了从.txt文件创建多个嵌套词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建由多个字典组成的字典.我是通过.txt文件创建的:
I am trying to create a dictionary consisting of multiple dictionaries. I am creating this from a .txt file:
chrY 6 8 +
chrY 3 5 +
chrX 10 11 +
chrX 13 15 -
我想要的输出将是:
{'chrY': {'+' : {'start': [3 , 6], 'end': [5, 8]}}, 'chrX': {'+' : {'start': [10], 'end': [11]} , '-': {'start' : [13], 'end' : [15]}}}
到目前为止,我的代码包括:
My code so far consists of:
import sys
first_dict = {}
intron_dict = {}
def main():
with open(sys.argv[1], 'r') as intron:
for line in intron.readlines():
line = line.split()
chromosome = line[0]
start = line[1]
end = line[2]
strand = line[3]
first_dict = {chromosome : (strand, start, end)}
for k, v in first_dict.iteritems():
intron_dict.setdefault(k, []).append(v)
print (intron_dict)
if __name__=='__main__':
main()
此代码使我可以对chrY和chrX键进行排序,而不会覆盖值.我在合并"+"和-"键并将数据转换为所需格式时遇到问题.到目前为止,我的输出看起来像:
This code allows me to sort the chrY and chrX keys without overwriting the values. I am having problems merging the "+" and "-" keys and getting the data into my desired format. So far my output looks like :
{'chrY': [('+', '6', '8'), ('+', '3', '5')], 'chrX': [('+', '10', '11'), ('-', '13', '15')]}
推荐答案
这里是没有defaultdict
的另一种方法.只需使用if ... else
Here is another method without defaultdict
. Just using if ... else
import sys
intron_dict = dict()
def main():
with open(sys.argv[1], 'r') as intron:
for line in intron.readlines():
line = line.split()
chromosome = line[0]
start = int(line[1]) # converted to int to avoid quotes in result
end = int(line[2])
strand = line[3]
first_dict = {strand : {'start' : [start], 'end' : [end]}}
if intron_dict.has_key(chromosome):
if intron_dict[chromosome].has_key(strand):
intron_dict[chromosome][strand]['start'].append(start)
intron_dict[chromosome][strand]['end'].append(end)
else:
intron_dict[chromosome][strand] = first_dict[strand]
else:
intron_dict.setdefault(chromosome, first_dict)
print (intron_dict)
if __name__=='__main__':
main()
输出:
{'chrY': {'+': {'start': [6, 3], 'end': [8, 5]}}, 'chrX': {'+': {'start': [10], 'end': [11]}, '-': {'start': [13], 'end': [15]}}}
这篇关于从.txt文件创建多个嵌套词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文