I am producing an average value from the difference of 3 values and want to place it in a list
A sample of the list I want to average looks like this:
...
[6.0, 270.0, -55.845848680633168],
[6.0, 315.0, -47.572000492889323],
[6.5, 0.0, -47.806802767243724],
[6.5, 45.0, -48.511643275159528],
[6.5, 90.0, -45.002053150122123],
[6.5, 135.0, -51.034656702050455],
[6.5, 180.0, -53.266356523649002],
[6.5, 225.0, -47.872632929518339],
[6.5, 270.0, -52.09662072002746],
[6.5, 315.0, -48.563996448937075]]
There will be up to 3 rows where the first 2 columns match (these 2 columns are polar coordinates) and when this is the case I want to take the difference between the 3rd elements, average it and append the polar coordinates of the point and the averaged result into a new list
for a in avg_data:
comparison = []
for b in avg_data:
if a[0] == b[0] and a[1] == b[1]:
comparison.append(b[2])
print comparison
z = 0 # reset z to 0, z does not need set now in if len(comp) == 1
if len(comparison) == 2: # if there are only 2 elements, compare them
z += -(comparison[0]) + comparison[1]
if len(comparison) == 3: # if all 3 elements are there, compare all 3
z += -(comparison[0]) + comparison[1]
z += -(comparison[0]) + comparison[2]
z += -(comparison[1]) + comparison[2]
z = z/3 #average the variation
avg_variation.append([a[0], a[1], z]) #append the polar coordinates and the averaged variation to a list
This code outputs the correct data to the list except it outputs it every time it comes across matching polar coordinates so I end up with duplicate rows.
To stop this I have tried implementing an if statement to look for matching polar coordinates in the avg_variation list before performing the averaging again
if a[0] not in avg_variation and a[1] not in avg_variation:
This does not work and I get the error
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I don't think any or all are what I am looking for as I only want to check the first two columns and not the third column against the already appended values. Anyone any idea how I can make my if statement better?
To clear up a bit more what my actual question is:
My code searches through nested lists for lists where the 1st 2 elements match, performs a calculation on the 3rd elements and then appends them to a new list. My problem is that if there are 2 or 3 rows where the 1st 2 elements match up it appends the result to the new list 2 or 3 times, I want it to only do it once
Edit: Sorry my original question was misleadng as to the purpose of my code.