Michael Wish
Michael Wish

Reputation: 11

I am trying to iterate through keys and update values, but ALL of my data is getting stored for EVERY key

Previously loaded are data files called hud_data and ahrs2_data. Code is as follows:

# Create keys for UAV dictionary with vehicles
UAV_dict = {}
for i in range(1, 6):
    UAV_dict["veh_{0}".format(i)] = []

# Create empty vehicle dictionaries
veh1_dict = {}
veh2_dict = {}
veh3_dict = {}
veh4_dict = {}
veh5_dict = {}
veh_list = [veh1_dict, veh2_dict, veh3_dict, veh4_dict, veh5_dict]

# Put empty dictionaries into UAV dictionary
i = 0
for key in UAV_dict:
    UAV_dict[key] = veh_list[i]
    i += 1
 

# Create keys from data and update dictionaries
keys = {'time': [], 'aspd': [], 'gspd': [], 'hdg': [], 'alt': [], 'time2': [], 'lat': [], 'lon': []}
for key in UAV_dict:
    UAV_dict[key].update(keys)

# Loop through vehicles in UAV dict and append appropriate data to vehicle dictionaries
for a_key in UAV_dict.keys():
    for data in hud_data.get(a_key):
        UAV_dict[key]['time'].append(data[0]) 
        UAV_dict[key]['aspd'].append(data[1]) 
        UAV_dict[key]['gspd'].append(data[2]) 
        UAV_dict[key]['hdg'].append(data[3]) 
        UAV_dict[key]['alt'].append(data[4]) 
    for data in ahrs2_data.get(key):
        UAV_dict[key]['time2'].append(data[0]) 
        UAV_dict[key]['lat'].append(data[1]) 
        UAV_dict[key]['lon'].append(data[2])

I am attempting to build the data inside the dictionary, but the data for EVERY vehicle is getting stored under EVERY key. For example, the actual length of veh_1 (first key) for 'time' is 11424, but when I run the code above the length is 55987, which is clearly every time value for all 5 vehicles. Why is all of the data being stored under every key?

Sample data: vehicle time aspd gspd hdg alt veh_1 17:19.5 0.16 0.14 213 273.89

Upvotes: 0

Views: 59

Answers (0)

Related Questions