파이썬에서 for 루프를 사용하여 사전을 만들고 싶습니다. 여기서 각 키 (내 경우 CUI)는 값 배열과 연관되어 있지만 얻은 결과는 각각 사전입니다 키가 내 목록에있는 값 중 하나에 불과합니다. 내 코드에 따라 :for 루프 파이썬에서 사전
import numpy as np
data2 = open('pathways.dat', 'r', errors = 'ignore')
pathways = data2.readlines()
special_line_indexes = []
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:
PWY_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] = PWY_ID
print(PWY_ID)
print (dict_comp)
편집 당신이 이상의 값을을 writting 때문에 그것의 이유 :
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'에 대한 내부 전에 빈 상태 (empty)의 배열이,'dict_comp의 [의 CUI] = PWY_ID'해야한다 '. 또한 구조에 따라 for 루프의 어딘가에서이 목록을 삭제해야합니다. – ZdaR