输入两个单独的列表并输出单个词典列表

时间:2019-03-09 05:57:08

标签: python python-3.x dictionary

我有以下列表:

l1=['R21', 'R21', 'R21', 'R22', 'R22', 'R22', 'R23', 'R23', 'R23']

l2=['T/T/T/T', 'G/G/G/G', 'CA/CA/CA/CA', 'G/G/G/G', 'TA/TA/TA/TA', 'T/T/T/T', 'TAA/TAA/TAA/TAA', 'C/C/C/C', 'T/T/T/T']

我想要这个作为输出:

l3=[{'R21': 'T/T/T/T'}, {'R21': 'G/G/G/G'}, {'R21': 'CA/CA/CA/CA'}, {'R22': 'G/G/G/G'}, {'R22': 'TA/TA/TA/TA'}, {'R22': 'T/T/T/T'}, {'R23': 'TAA/TAA/TAA/TAA'}, {'R23': 'C/C/C/C'}, {''R23': 'T/T/T/T'}]

这可能吗?

1 个答案:

答案 0 :(得分:1)

使用以下简单的列表理解:

l3 = [{k: v} for k,v in zip(l1, l2)]

现在:

print(l3)

是:

[{'R21': 'T/T/T/T'}, {'R21': 'G/G/G/G'}, {'R21': 'CA/CA/CA/CA'}, {'R22': 'G/G/G/G'}, {'R22': 'TA/TA/TA/TA'}, {'R22': 'T/T/T/T'}, {'R23': 'TAA/TAA/TAA/TAA'}, {'R23': 'C/C/C/C'}, {''R23': 'T/T/T/T'}]