I have a dictionary with arrays (same length) associated to strings. My goal is to create a new dictionary with the same keys but cutting the arrays, keeping only the elements I need. I wrote a function to do it but the problem is that it returns a dictionary with the same array (correct cut length) associated to every key, while the print command i put to control show the correct association. Here's the function:
def extract_years(dic,initial_year,final_year):
dic_extr = {}
l = numpy.size(dic[dic.keys()[0]])
if final_year != 2013 :
a = numpy.zeros((final_year - initial_year)*251)
elif final_year == 2013 :
a = numpy.zeros(l - (initial_year-1998)*251)
for i in range(0,len(dic)):
#print i
for k in range (0,numpy.size(a)):
a[k] = dic[dic.keys()[i]][(initial_year-1998)*251 + k]
#print k
dic_extr[dic.keys()[i]] = a
print dic.keys()[i]
print dic_extr[dic.keys()[i]]
print dic_extr.keys()
print dic_extr
return dic_extr
as I said, print dic_extr[dic.keys()[i]] shows the correct results while the final print dic_extr shows a dictionary with the same array associated to every key.
print dic.keys()[i] print dic_extr[dic.keys()[i]]while the last isprint dic_extrwhich is completely different, i just expected the whole first print all together in the new dictionary