私は、各キー(私の場合は 'CUI')の値の配列に関連付けられているforループを使用してPythonで辞書を作成したいが、私が得る出力は辞書ですキーは私のリストの値のうちの1つだけを返します。私のコードを次に示します。辞書for forループのpython
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'持つたびに、内側の前に空の配列を取得し
あなたはPWY_ID'に値を追加しているので、 'dict_compの[のCUI] = PWY_ID'する必要があります'。また、構造体ごとにforループのどこかでこのリストをクリアする必要があります。 – ZdaR