خرید بک لینک

Vote count: 0

I have some code I'm analyzing. But I've found that iterating over a dictionary empties it. I've fixed the problem by by making a deepcopy of the dictionary and iterating over that in some code that displays the values, then later use the original dictionary to iterate over that to assign values to a 2D array. Why does iterating over the original dictionary to display it empty it, so that later use of the dictionary is unusable since it is now empty? Any replies welcome.

import copy

# This line fixed the problem
trans = copy.deepcopy(transitions)

print ("nTransistions  = ")
# Original line was:
#  for state, next_states in transitions.items():
# Which empties the dictionary, so not usable after that

for state, next_states in trans.items():
    for i in next_states:
        print("nstate = ", state, " next_state = ", i)

# Later code which with original for loop showed empty dictionary
for state, next_states in transitions.items():
    for next_state in next_states:
        print("n one_step trans  state = ", state, " next_state = ", next_state)
        one_step[state,next_state] += 1

asked 1 min ago

برچسب: python iterate over dictionary,python iterate over list,python iterate over array,python iterate over files in directory,python iterate over two lists,python iterate over set,python iterate over string,python iterate over list with index,python iterate over lines in file,python iterate over directory, نویسنده: استخدام کار تاريخ: شنبه 30 مرداد 1395 ساعت: 7:01

صفحه بندی