列表嵌套在字典中

时间:2017-11-02 23:03:39

标签: python file dictionary defaultdict

所以我正在尝试开发一种方法来在字典中存储4个不同的类别(让我们称之为A,B,C,D),以便我可以访问每个字典中的键/值并检查它们文件名。到目前为止,我已经能够将这些类别中的3个存储在字典中,而不是第4个。这些类别来自excel文件,然后复制到常规.txt文件(我已经包含了.txt文件)。有没有办法在我的字典中添加第4个组件?

链接.txt文件:https://drive.google.com/file/d/0B2s43FKt5BZgQldULXVOR0RBeTg/view?usp=sharing

这是我的剧本:

from collections import defaultdict
source_file = <file path>-<file name>.txt
data_set = defaultdict(list)    #sets up a defaultdict because there may be multiple overlapping keys
s = [b for b in [i.strip('\n').split('\t') for i in open(source_file)] if b]  # removes new line & tab spaces in .txt file
for a, b, c, d in s: # a is donor, b is barcode, c is batch, d is donor
  if a == 'Component1':  # We don't want to save the column headings
    pass
  else:
    data_set[a].append({b: c})  # creates the default dictionary

目前的输出是这样的:

{'1':[{'ab':'tg'},{'dd':'dd'}],'2':{'dc':'yh'},3:{'we':'hh'}}

1 个答案:

答案 0 :(得分:1)

您可以将列存储为元组:

import csv
from collections import defaultdict
source_file = "<file path>-<file name>.txt"
data_set = defaultdict(list)
with open(source_file) as f:
    lines = csv.reader(f, delimiter='\t')
    _ = next(lines) # skip header
    for donor, barcode, batch, donor2 in lines:
        data_set[a].append((barcode, batch, donor2))  # save columns as tuple