Comparing two types of lists in Python
Question:
I have two lists J
and Cond
. I want to compare these lists and print corresponding values in J
for False
in Cond
. Is there equivalent of np.where()
? I present the expected output.
J=[[1,2,4,6,7]]
Cond = [False, True, False, True, False]
The expected output is
J=[1,4,7]
Answers:
You can use the zip
builtin:
J = [zipped_val[0] for zipped_val in zip(J[0], Cond) if not(zipped_val[1])]
print(J)
Which results in:
[1, 4, 7]
you can do:
[j for j, cond in zip(J[0], Cond) if not cond]
I have two lists J
and Cond
. I want to compare these lists and print corresponding values in J
for False
in Cond
. Is there equivalent of np.where()
? I present the expected output.
J=[[1,2,4,6,7]]
Cond = [False, True, False, True, False]
The expected output is
J=[1,4,7]
You can use the zip
builtin:
J = [zipped_val[0] for zipped_val in zip(J[0], Cond) if not(zipped_val[1])]
print(J)
Which results in:
[1, 4, 7]
you can do:
[j for j, cond in zip(J[0], Cond) if not cond]