2015-11-25 110 views
1

我已經創建了用於計算氨基酸頻率的列表和字典。使用列表中的元素更新字典的值Python

我想基本上用列表中的值替換字典中的值。

因此,例如,

我現在擁有的是。

L1 = [0.0429, 0.0071, 0.05, 0.0929, 0.0429, 0.0143, 0.0071, 0.0429, 0.0929, 0.1143, 0.0643, 0.0643, 0.05, 0.0286, 0.0286, 0.0643, 0.0857, 0.0714, 0.0143, 0.0214]

D1 = OrderedDict([('A', 6), ('C', 1), ('D', 7), ('E', 13), ('F', 6), ('G', 2), ('H', 1), ('I', 6), ('K', 13), ('L', 16), ('M', 9), ('N', 9), ('P', 7), ('Q', 4), ('R', 4), ('S', 9), ('T', 12), ('V', 10), ('W', 2), ('Y', 3)])

我想在D1更換每一個值值L1。 因此,例如我想要([('A', 0.0429),('C',0.0071)....等。

這是創建列表(L1)和字典(D1)的代碼。

for seq_record in SeqIO.parse(f1,'fasta'): 

     sorted_dict = (collections.OrderedDict(sorted(Counter(seq_record.seq).items()))) 
     total = float(sum(Counter(seq_record.seq).values())) 
     print sorted_dict 
     aa_frequency =(round(value/total,4) for key, value in sorted_dict.items()) 
     aa_frequency_value = [] 
     for i in aa_frequency: 
      aa_frequency_value.append(i) 

回答

3

很簡單,因爲你已經有了值和鍵的順序。

for val, key in zip(L1, D1): 
    D1[key] = val 

好像你或許應該只是這樣做原地的,因爲你不添加或刪除元素

total = float(um(Counter(seq_record.seq).values())) 
for key,val in sorted_dict.items(): 
    newval = round(val/total, 4) 
    sorted_dict[key] = newval 
+0

感謝,這正是我想要的東西, – Danny

相關問題