Я перебираю элементы словаря "var_dict".Затем, когда я повторяю цикл for, мне нужно обновить словарь.Я понимаю, что это невозможно, и это вызывает ошибку времени выполнения, которую я испытал.
Мой вопрос: нужно ли создавать другой словарь для хранения данных?Как и сейчас, я пытаюсь использовать один и тот же словарь с разными ключами.
Я знаю, что проблема связана с итерацией по ключу и значениям словаря и попыткой изменить его.Я хочу знать, если лучший вариант в этом случае, если создать отдельный словарь.
for k, v in var_dict.items():
match = <b>str</b>(match)
match = match.strip("[]")
match = match.strip("''")
result = [index for index, value in <b>enumerate</b>(v) if match in value]
result = <b>str</b>(result)
result = result.strip("[]")
result = result.strip("'")
#====> IF I print(var_dict), at this point I have no error *********
if result == "0":
#It means a match between interface on RP PSE2 model was found; Interface position is on PSE2 architecture
<b>print</b> (f'PSE-2 Line cards:{v} Interfaces on PSE2:{entry} Interface PortID:{port_id}')
port_id = <b>int</b>(port_id)
<b>print</b>(port_id)
if port_id >= 19:
#print(f'interface:{entry} portID={port_id} CPU_POS={port_cpu_pos} REPLICATION=YES')
if_info = [entry,'PSE2=YES',port_id,port_cpu_pos,'REPLICATION=YES']
var_dict['IF_PSE2'].append(if_info)
#===> *** This is the point that if i attempt to print var_dict, I get the Error during olist(): dictionary changed size during iteration
else:
#print(f'interface:{entry},portID={port_id} CPU_POS={port_cpu_pos} REPLICATION=NO')
if_info = [entry,'PSE2=YES',port_id,port_cpu_pos,'REPLICATION=NO']
var_dict['IF_PSE2'].append(if_info)
else:
#it means the interface is on single PSE. No replication is applicable. Just check threshold between incoming and outgoing rate.
if_info = [entry,'PSE2=NO',<b>int</b>(port_id),port_cpu_pos,'REPLICATION=NO']
var_dict['IF_PSE1'].append(if_info)