我有一个唯一字符串列表(“示例ID”)。我还有一个表,其中包含第一个列表中的字符串子集,每个字符串都与下一列中的另一个字符串(“示例特征”)相关联(用空格作为分隔符)。例如:
# All Sample IDs
id-001
id-002
id-003
id-004
id-005
你知道吗
# Subset of Samples, with associated characteristics string
id-001 'batch-1, yellow'
id-003 'batch-1, yellow'
id-005 'batch-9, blue'
# Desired Output
id-001 'batch-1, yellow'
id-002 NA
id-003 'batch-1, yellow'
id-004 NA
id-005 'batch-9, blue'
我正在尝试合并这两个列表,创建一个表,其中第一列将包含所有“sample ID”,第二列将包含每个ID对应的“sample characteristics”字符串,如果ID不在第二个列表中,则为“NA”。你知道吗
我一直在使用这段代码比较两个ID列表,以找出哪些示例ID将具有可用的“sample characteristics”字符串:
with open('FILE1.txt', 'r') as file1:
with open('FILE2.txt', 'r') as file2:
same = set(file1).intersection(file2)
with open('RESULT.txt', 'w') as file_out:
for line in same:
file_out.write(line)
我还没有弄清楚如何获得这些id的“样本特征”,并将它们与第一个列表结合起来。我认为使用dict应该是第一步:
with open('FILE1.txt', 'r') as file1, open('FILE2.txt', 'r') as file2:
data1 = file1
data2 = dict(file2)
我不知道如何继续。你知道吗
我想你在找这样的东西:
这基本上建立了一个dict,将第一个文件中的所有id作为dict的键。你知道吗
然后遍历第二个文件的每一行,为出现的每个id更新dict。你知道吗
然后它将更新后的dict写入一个新的csv文件。你知道吗
相关问题 更多 >
编程相关推荐