Removing elements of one list with respect to another list in Python
Question:
I have two lists A
and B
. I am removing element from A
which is less than tol
. But I also want to remove element from B
corresponding to the removed element from A
. For example, [[2.22105075e-18]]
was removed from A
. Corresponding to this, [[4,13]]
should be removed from B
. I present the current and expected output.
import numpy as np
A= [[9.16435586e-05], [0.000184193464], [9.28353239e-05], [2.22105075e-18]]
B= [[13, 14], [4, 5], [5, 14], [4, 13]]
tol=1e-12
CA=[[x[0]] for x in A if x[0]>tol]
CB=[x for x,y in enumerate(CA) if list(y) in B]
print(CB)
The current output is
[]
The expected output is
[[13, 14], [4, 5], [5, 14]]
Answers:
You could process the two in parallel with zip
:
CA, CB = map(list, zip(*((a, b) for a, b in zip(A, B) if a[0]>tol)))
# or
# CA, CB = map(list, zip(*(x for x in zip(A, B) if x[0][0]>tol)))
Output:
# CA
[[9.16435586e-05], [0.000184193464], [9.28353239e-05]]
# CB
[[13, 14], [4, 5], [5, 14]]
The basic idea to achive your stated goal is as follows:
- Pair up both lists
A
and B
with zip
to give pairs of the form (a, b)
.
- Keep only the bs for meach
a b
pair which satisfy the predicate on a
.
We can implement this several ways.
Comprehension implementation
A= [[9.16435586e-05], [0.000184193464], [9.28353239e-05], [2.22105075e-18]]
B= [[13, 14], [4, 5], [5, 14], [4, 13]]
tol=1e-12
result = [b for a,b in zip(A, B) if a[0]>tol]
Explicit loop implementation
result = []
for i in range(len(A)):
if A[i][0] > tol:
result.append(B[i])
Both methods essentially implement the outlined algorithm.
I have two lists A
and B
. I am removing element from A
which is less than tol
. But I also want to remove element from B
corresponding to the removed element from A
. For example, [[2.22105075e-18]]
was removed from A
. Corresponding to this, [[4,13]]
should be removed from B
. I present the current and expected output.
import numpy as np
A= [[9.16435586e-05], [0.000184193464], [9.28353239e-05], [2.22105075e-18]]
B= [[13, 14], [4, 5], [5, 14], [4, 13]]
tol=1e-12
CA=[[x[0]] for x in A if x[0]>tol]
CB=[x for x,y in enumerate(CA) if list(y) in B]
print(CB)
The current output is
[]
The expected output is
[[13, 14], [4, 5], [5, 14]]
You could process the two in parallel with zip
:
CA, CB = map(list, zip(*((a, b) for a, b in zip(A, B) if a[0]>tol)))
# or
# CA, CB = map(list, zip(*(x for x in zip(A, B) if x[0][0]>tol)))
Output:
# CA
[[9.16435586e-05], [0.000184193464], [9.28353239e-05]]
# CB
[[13, 14], [4, 5], [5, 14]]
The basic idea to achive your stated goal is as follows:
- Pair up both lists
A
andB
withzip
to give pairs of the form(a, b)
. - Keep only the bs for meach
a b
pair which satisfy the predicate ona
.
We can implement this several ways.
Comprehension implementation
A= [[9.16435586e-05], [0.000184193464], [9.28353239e-05], [2.22105075e-18]]
B= [[13, 14], [4, 5], [5, 14], [4, 13]]
tol=1e-12
result = [b for a,b in zip(A, B) if a[0]>tol]
Explicit loop implementation
result = []
for i in range(len(A)):
if A[i][0] > tol:
result.append(B[i])
Both methods essentially implement the outlined algorithm.