0

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

  • Most of the code you shared is pointless or inefficient moving around of the same data. Why are you trying to create a dictionary of 'columns', with each column just creating a series of values for that attribute? Is there a reason you're not using `pandas` which would allow you to do the above in one or two lines, and faster at that? What is the data format/type of `hud_data` and `ahrs2_data`? – Grismar Nov 24 '21 at 04:23
  • Because I'm new at this and trying to learn dictionaries. I have two csv files (hud and ahrs2) which I've imported as dictionaries. The keys for each are the UAVs (vehicles 1 through 5). Each csv has columns of data (airspeed, groundspeed, etc.). I was trying to build a single dictionary (UAV_data) that has keys for the vehicles, then a nested dictionary associated with each key for the data. I can do all this easily with lists and 2-d lists, but I'm trying to learn how dictionaries work. I'm not yet familiar with pandas. – Michael Wish Nov 24 '21 at 04:32
  • Can you share (part of) the data you have in `hud_data` and/or `ahrs2_data`? – Grismar Nov 24 '21 at 04:37
  • Please put a sample of the data in the question, not in the comments – Grismar Nov 24 '21 at 04:44

0 Answers0