I have an empty dictionary (tried both completely empty and with keys from list - same results).
I am then trying to iterate through that same list (in this case rooms in a building), pull some data from another source into a class instance, and then set the value in the dictionary designated for that room to the class instance. However, each one overwrites the last, so at the end every value (class) in the dictionary is identical and comes from the last room.
I'm sure this comes from my lack of understanding of references, however I've tried copying and deep copying but not luck. Any suggestions? Here's a summary of my code. I can provide more details if they are relevant, such as the class definition, but was trying to keep it succinct:
# Initialize dict with keys but no values
results = dict.fromkeys(rooms)
for room_id in rooms: # rooms is a list of strings
# Instantiate class for results data
results_data = LoadResultsData
# Pull data for each variable and assign and divide to change units
results_data.sens_total = np.array([1, 2, 3, 4, 5])
results_data.sens_lighting = np.array([2, 2, 3, 4, 5])
results_data.sens_equipment = np.array([3, 2, 3, 4, 5])
results_data.sens_people = np.array([4, 2, 3, 4, 5])
# Assign class to overall dictionary with ID as key
results[room_id] = results_data
Thank you.
if LoadResultsData
is a class you need the ()
(parenthesis). and your list of strings (room_ids
) are unique?
from pprint import pp
import numpy as np
class LoadResultsData:
def __init__(self):
self.name = ''
rooms = ['yellow', 'green', 'blue', 'purple', 'red']
results = {}
for room_id in rooms: # rooms is a list of strings
# Instantiate class for results data
results_data = LoadResultsData()
# Pull data for each variable and assign and divide to change units
results_data.sens_total = np.array([1, 2, 3, 4, 5])
results_data.sens_lighting = np.array([2, 2, 3, 4, 5])
results_data.sens_equipment = np.array([3, 2, 3, 4, 5])
results_data.sens_people = np.array([4, 2, 3, 4, 5])
# Assign class to overall dictionary with ID as key
results_data.name = room_id
results[room_id] = results_data
pp(results)
print('---')
for k, v in results.items():
pp([(id(v), k), [(i, getattr(v, i)) for i in dir(v) if i.startswith('sens')]])
output:
{'yellow': <__main__.LoadResultsData object at 0x7f766ea87430>,
'green': <__main__.LoadResultsData object at 0x7f766ea5bfa0>,
'blue': <__main__.LoadResultsData object at 0x7f766e98aa90>,
'purple': <__main__.LoadResultsData object at 0x7f766d92e3a0>,
'red': <__main__.LoadResultsData object at 0x7f766d920640>}
---
[(140146639402032, 'yellow'),
[('sens_equipment', array([3, 2, 3, 4, 5])),
('sens_lighting', array([2, 2, 3, 4, 5])),
('sens_people', array([4, 2, 3, 4, 5])),
('sens_total', array([1, 2, 3, 4, 5]))]]
[(140146639224736, 'green'),
[('sens_equipment', array([3, 2, 3, 4, 5])),
('sens_lighting', array([2, 2, 3, 4, 5])),
('sens_people', array([4, 2, 3, 4, 5])),
('sens_total', array([1, 2, 3, 4, 5]))]]
[(140146638367376, 'blue'),
[('sens_equipment', array([3, 2, 3, 4, 5])),
('sens_lighting', array([2, 2, 3, 4, 5])),
('sens_people', array([4, 2, 3, 4, 5])),
('sens_total', array([1, 2, 3, 4, 5]))]]
[(140146621211552, 'purple'),
[('sens_equipment', array([3, 2, 3, 4, 5])),
('sens_lighting', array([2, 2, 3, 4, 5])),
('sens_people', array([4, 2, 3, 4, 5])),
('sens_total', array([1, 2, 3, 4, 5]))]]
[(140146621154880, 'red'),
[('sens_equipment', array([3, 2, 3, 4, 5])),
('sens_lighting', array([2, 2, 3, 4, 5])),
('sens_people', array([4, 2, 3, 4, 5])),
('sens_total', array([1, 2, 3, 4, 5]))]]