I have four lists representing X and Y coordinates of different points in lists A and B so X_A, Y_A, X_B, Y_B
A ] ['a', 'b', 'c', 'd', 'e', 'f']
X_A = [1.0, 2.2, 3.0, 4.0, 5.001, 6.0]
Y_A = [0.0, 2.0, 4.0, 6.1, 8.0, 0.0]
B = ['k', 'l', 'm', 'n', 'o', 'p']
X_B = [1.0, 3.0, 5.0005, 7.0, 9.1, 11.0]
Y_B = [0.0, 1.0, 8.0, 2.0, 8.0, 0.1]
The above is read as 'a' has coordinates (1.0, 0.0) and 'l' has coordinates (3.0, 1.0)
I would like to add items from A to Node_A list and items from B to Node_B the corresponding coordinates of the elements of lists A and B satisfy an error margin:
(X_A - X_B) <= error and (Y_A - Y_B) <= error
I have the below code which I believe should be doing the job but it does not seem to work and I don't understand why, any help is appreciated!
Node_A, Node_B = [], []
error = 1e-2
i, j = 0, 0
for lo1,la1 in zip(X_A, Y_A):
for lo2,la2 in zip(X_B, Y_B):
if abs(lo1 - lo2) <= error and abs(la1 == la2) <= error:
Node_A.append(A[i])
Node_B.append(B[j])
break
j += 1
i += 1
j = 0
The desired output should be as below as they 'a' coordinates from A equal 'k' coordinates from B and 'e' coordinates satisfy the error margin and thus approximately equal to 'm' coordinates:
Node_A: ['a', 'e']
Node_B: ['k', 'm']
Instead I get Node_A: ['c'] and Node_B: ['l']
Change
if abs(lo1 - lo2) <= error and abs(la1 == la2) <= error:
to
if abs(lo1 - lo2) <= error and abs(la1 - la2) <= error:
Result:
['a', 'e']
['k', 'm']
You made a typo in the second condition.
This gives you a:e,k:m:
i, j = 0, 0
for lo1,la1 in zip(X_A, Y_A):
for lo2,la2 in zip(X_B, Y_B):
if abs(lo1 - lo2) <= error and abs(la1 - la2) <= error:
Node_A.append(A[i])
Node_B.append(B[j])
break
j += 1
i += 1
j = 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.