I want to create a dictionary in python using a for loop, where each key ('CUI' in my case)is associated with an array of values, but the output I obtain is a dictionary where each key yeld just one of the values in my list. Following my code:
import numpy as np
data2 = open('pathways.dat', 'r', errors = 'ignore')
pathways = data2.readlines()
special_line_indexes = []
PWY_ID = []
line_cont = []
L_PRMR = [] #Left primary
dict_comp = dict()
#i is the line number (first element of enumerate), while line is the line content (2nd elem of enumerate)
for CUI in just_compound_id:
for i,line in enumerate(pathways):
if '//' in line:
#fint the indexes of the lines containing //
special_line_indexes = i+1
elif 'REACTION-LAYOUT -' in line:
if CUI in line:
PWY_ID.append(special_line_indexes)
dict_comp[CUI] = special_line_indexes
print(PWY_ID)
dict_comp[CUI] = special_line_indexes, should bedict_comp[CUI] = PWY_ID, since you have been appending value toPWY_ID. Also you need to clear this list somewhere in the for loops as per your structure.