I have two lists Z1 and Z2. I am comparing these two lists and if at least one sublist of Z1 appears in Z2, I want to print full Z1. I present the current and expected output.
Z1=[[0, 4], [0, 5], [4, 5]]
Z2=[[6, 5], [4, 9], [4, 8], [5, 3], [0,4]]
for i in range(0,len(Z1)):
if (Z1[i]==Z2[i]):
print("Z1 =",Z1)
The current output is
(Blank)
The expected output is
Z1=[[0, 4], [0, 5], [4, 5]]
>Solution :
An rather elegant solution can be using sets and their intersection:
Z1=[[0, 4], [0, 5], [4, 5]]
Z2=[[6, 5], [4, 9], [4, 8], [5, 3], [0,4]]
zs1 = set(map(tuple, Z1))
zs2 = set(map(tuple, Z2))
if (zs1 & zs2):
print("Z1 =",Z1)