I have a set of x,y coordinates and I want to get the largest coordinate value which have the difference less than 0.1.
For example; I have an array which contains both x,y values as below:
array = [6.72, 0.16,13.28, 0.14,13.3, 5.64,6.74, 5.68,6.62, 0.14]
x values -> 6.72,13.28,13.3,6.74,6.62
y values -> 0.16,0.14,5.64,5.68,0.14
If I take x values; 6.72,6.74,6.62 values have difference less than 0.1.
So I want to take the largest value of them. It's 6.74. 13.3, 13.28 also has a difference less than 0.1. So I need to take the largest value, 13.3.
I need to replace 6.72,6.74,6.62 from 6.74 and 13.3, 13.28 from 13.3
I need to do the same thing for y values as well.
My output array should be;
output_array = [6.74, 0.16,13.3, 0.16,13.3, 5.68,6.74, 5.68,6.74, 0.16]
You can iterate through sorted coordinate values and keep only the largest of numbers that differ from the preceding numbers by less than 0.1, and map the preceding numbers to the largest numbers.
coords = [6.72, 0.16, 13.28, 0.14, 13.3, 5.64, 6.74, 5.68, 6.62, 0.14]
last_coord = None
unmapped = []
mapping = {}
for coord in sorted(coords):
if last_coord is not None and coord - last_coord >= 0.1:
mapping.update(dict.fromkeys(unmapped, last_coord))
unmapped = []
unmapped.append(coord)
last_coord = coord
mapping.update(dict.fromkeys(unmapped, last_coord))
coords = list(map(mapping.get, coords))
print(coords)
This outputs:
[6.74, 0.16, 13.3, 0.16, 13.3, 5.68, 6.74, 5.68, 6.74, 0.16]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments