Extracting data out of Complex JSON - python

This is the JSON data I am using
In this dataset, I want to extract the value of "yaw" and store it into in camera_loc. My first attempt for the code was as follows:
with open(
"/home/siddhant/catkin_ws/src/view-adaptation/multi_sensors.json"
) as sensors:
multi_sensors = json.load(sensors)
camera_loc = [
multi_sensors["sensors"][0]["yaw"],
multi_sensors["sensors"][1]["yaw"],
multi_sensors["sensors"][2]["yaw"],
multi_sensors["sensors"][3]["yaw"],
multi_sensors["sensors"][4]["yaw"],
multi_sensors["sensors"][5]["yaw"],
]
This gives me the expected result.
But I want to generalize the same for any number of entries in the 'sensors' array.
I tried executing a 'for' loop and extracting the values for the same as follows:
for i in multi_sensors["sensors"]:
camera_loc = []
camera_loc.append(i["yaw"])
However, this method only gives a single value in the camera_loc list which is the last 'yaw' value from the JSON file. I am looking for a better approach or even any modifications to the way I execute the loop so that I can extract all the values of 'yaw' from the JSON file - in this example there are 6 entries but I want to generalize it for 'n' entries that may be created in other cases.
Thank you!

That's because you're defining your camera_loc array inside the loop, meaning every iteration resets it to []. This code should work if you remove the array definition from the loop:
camera_loc = []
for i in multi_sensors["sensors"]:
camera_loc.append(i["yaw"])
This is also a perfect use case for a List Comprehension:
camera_loc = [i["yaw"] for i in multi_sensors["sensors"]]
Both answers results in the same array though, so you may choose whichever you like most.

Related

Someone please explain what the for loop does / its meaning for this section of code?

I'm learning python and I'm trying to understand what the for loop means. My assignment is to understand and comment the full code out. I know that the first two lines of code take an input. Can someone tell me what the next lines of code mean? The for loop is extremely confusing. By the way, let's say the input for j_t is 1, 3, 5 and the input for d_t is 2, 4, 6. Hopefully that helps.
# to take inputs
j_t = list(map(int, input("Process times: ").split(",")))
d_t = list(map(int, input("Job Due Dates: ").split(",")))
dict_spt = {}
dict_edd = {}
for i in range(len(j_t)):
dict_spt[int(j_t[i])] = int(d_t[i])
dict_edd[int(d_t[i])] = int(j_t[i])
The above is a segment of the full code. I ran the full code and it allows me to put in input but I need to understand what the for loop actually does/means in plain english.
Python input give you a string input, j_t and d_t are lists made from your inputs but it is still as string in the lists. for loop is actually iterating on your inputs to create dictionnaries which allow you to get j_t from a d_t value and vice versa and in a same time is casting all inputs to integers to make it easier to use i guess.
Hope it is clear enough
The first 2 lines of code take in an input and creates a list out of them (j_t and d_t).
The next 2 lines initializes a dictionary object (dict_spt and dict_edd).
The for loop iterates from 0 to the length of j_t - 1 = 2 which means that it iterates over all the indices of the items in j_t (1,3,5). The first line in the dictionary puts the key-value pair of int(j_t[0]) = 1 , int(d_t[0]) = 2 into the dict_spt dictionary. And it also adds in the key-value pair of int(d_t[0]) = 2 , int(j_t[0]) = 1 (which is same like the first one but reversed) into the dict_edd dictionary. It does this until the last element (5) is reached.
Hopefully this explanation is clear.
The for loop is your standard construct for iterating through arrays of data. I'll walk you through the whole code.
So in python you have multiple types of, let's call them "array like" structures. In this example you've shown, you have lists and dictionaries. Lists are very similar to standard arrays, where as dictionaries are an "array" of key value pairs.
Now the first two lines take your comma separated inputs and turn them into lists.
Then you create two empty dictionaries - that's indicated by the curly brackets, those are used for defining dictionaries.
Then you have the actual for loop - the best way to read the for line would be:
"For each element i in the range() from zero to the length of thej_t list, do the following"
Important to note that i or whatever you put after the for keyword is a variable that is created on the fly simply for the purpose of iterating through the piece of data you're iterating.
And then in the for loop what you are doing is adding a new record to each dictionary where the key and the value are the integer value of each list element.
Arrays and lists have indexes for every element inside of them so all the standard for loop needs is a numerical range which would represent every index in the structure you're iterating through.
Hope this was clear enough.

How to merge several Python lists together using for loop

I am writing a script in order to calculate all the euclidean distances between a X value and a lot of other values in a dictonary, obtaining a float that, then, I convert in a list. The problem is I don't obtain a list with all the outcomes but many lists with only one element inside, my outcome.
My script for the moment is:
single_mineral = {}
for k in new_dict.keys():
single_mineral = new_dict[k]
Zeff = single_mineral["Zeff_norm"]
rhoe = single_mineral["Rhoe_norm"]
eucl_Zeff= (calculated_Zeff_norm, Zeff)
eucl_rhoe= (calculated_rhoe_norm, rhoe)
dst= [(distance.euclidean(eucl_Zeff, eucl_rhoe))]
print(dst)
I obtain something like that:
[0.29205348037179407]
[0.23436642937625374]
[0.3835446564476642]
[0.11616594912309205]
[0.21792958584034935]
and they are not linked somehow (so I can't use intertools.chain).
I want to create a single list with all these lists (the final goal is the ascending order...for this reason I need only one list).
I guess the solution is a for loop but I have no idea how to do it. I don't understand where it needs to run and how can I add my outcomes, which are always called "dst"?
Please, help me! Thank you very much in advance!
if you want to do get all in one list then you need
# before loop
dst = []
# loop
for k in new_dict.keys():
# ... code ...
#dst.append( [distance.euclidean(eucl_Zeff, eucl_rhoe)] )
dst.append( distance.euclidean(eucl_Zeff, eucl_rhoe) )
# after loop
print(dst)

Can I condense these values with a loop?

I have a set of values that get modified like so:
iskDropped = irrelevant1
iskDestroyed = irrelevant2
iskTotal = irrelevant3
iskDropped = condense_value(int(iskDropped[:-7].replace(',','')))
iskDestroyed = condense_value(int(iskDestroyed[:-7].replace(',','')))
iskTotal = condense_value(int(iskTotal[:-7].replace(',','')))
As you can see, all three lines go through the same changes. (condensed, shortened, and commas removed) before overwriting their original value.
I want to condense those three lines if possible because it feels inefficient.
I was trying something like this:
for value in [iskDropped,iskDestroyed,iskTotal]:
value = condense_value(int(value[:-7].replace(',','')))
which if you changed into a print statement successfully does print the correct values but it does not work in the regard of overwriting / updating the values (iskDropped,iskDestroyed, and iskTotal) that I need to call later in the program.
Is it possible to condense these lines in Python? If so can someone point me in the right direction?
You can do it like this:
iskDropped, iskDestroyed, iskTotal = [condense_value(int(value[:-7].replace(',',''))) for value in [iskDropped, iskDestroyed, iskTotal]]
This works by looping through the list of your 3 variables, performing the condense_value function on each and creates a list of the results, then finally unpacks the list back into the original values.

How to use set() during iteration?

How to use set() during iteration? I still have single value.
data = {'mynumber': set()}
example = Example.objects.filter(user=request.user)
for e in example:
data['mynumber'].add(e.mynumber)
print data #{'mynumber': set([15.0])}
try using setdefault:
When your condition satisfy, the following snippet will create an empty set for that number or key, so it will create if its not already exist else it will re-use:
data.setdefault('mynumber', set())
Based on original edit
You're adding to a set mynumber, which can contain only unique values, the results from a django query Example.objects.filter(mynumber=5) which only ever includes items where mynumber is 5... Ergo, you'll only ever end up with an empty set (no query results), or a set containing 5 (1 or more results).
Note
After you've traced through your code to check you're getting the values you're expecting, then you can get rid of the loop and just write your code as:
data = {'mynumber': set(Example.objects.filter(user=request.user).values_list('mynumber'))

Python: How can I find the differences between two lists of strings?

I'm using Python 3. I have two lists of strings and I'm looking for mismatches between the two. The code I have works for smaller lists but not the larger lists I'm writing it for.
Input from the non-working lists is in the following format:
mmec11.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec13.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec12.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec14.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
My function to compare two lists of data in the above format is:
result = []
for x in mmeList1:
if x not in mmeList2:
result.append(x)
return result
The problem is it's not working. I get an output file of both lists combined into one long list. When I put a test is to say "Hi" every time a match was made, nothing happened. Does anyone have any ideas where I'm going wrong. I work for a telecommunications company and we're trying to go through large database dumps to find missing MMEs.
I'm wondering if maybe my input function is broken? The function is:
for line in input:
field = line.split()
tempMME = field[0]
result.append(tempMME)
I'm not very experienced with this stuff and I'm wondering if the line.split() function is messing up due to the periods in the MME names?
Thank you for any and all help!
If you don't need to preserve ordering, the following will result in all mmes that exist in list2 but not list1.
result = list(set(mmeList2) - set(mmeList1))
I tested your compare function and it's working fine, assuming that the data in mmeList1 and mmeList2 is correct.
For example, I ran a test of your compare function using the following data.
mmeList1:
mmec11.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec13.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec12.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec14.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmeList2:
mmec11.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec13.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec12.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec15.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
Result contained:
mmec14.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
I suspect the problem is that mmeList1 and mmeList2 don't contain what you think they contain. Unfortunately, we can't help you more without seeing how mmeList1 and mmeList2 are populated.
If you want to see the differences in both, (i.e. Result should contain mmec14 AND mmec15), then what you want to use is Sets.
For example:
mmeSet1 = set(mmecList1)
mmeSet2 = set(mmecList2)
print mmeSet1.symmetric_difference(mmeSet2)
will result in:
['mmec14.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org', 'mmec15.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org']
At first, using set() on list is best way for decreasing iteration.Try this one
result = []
a=list(set(mmeList1))
b=list(set(mmeList2))
for x in a:
if x not in b:
result.append(x)
return result

Categories

Resources