我想遍历两个列表并检查list_1中的项是否在list_2中。如果list_1中的项在list_2中,则我希望打印list_2中的项。如果该项不在list_2中,则我希望打印list_1中的项。以下代码部分实现了这一点,但由于执行了两个for循环,所以我得到了list_1的重复值。请用Pythonic的方式指导我完成这个问题?
list_1 = ['A', 'B', 'C', 'D', 'Y', 'Z']
list_2 = ['Letter A',
'Letter C',
'Letter D',
'Letter H',
'Letter I',
'Letter Z']
for i in list_1:
for x in list_2:
if i in x:
print(x)
else:
print(i)
当前输出:
Letter A
A
A
A
A
A
B
B
B
B
B
B
C
Letter C
C
C
C
C
D
D
Letter D
D
D
D
Y
Y
Y
Y
Y
Y
Z
Z
Z
Z
Z
Letter Z
期望输出:
Letter A
B
Letter C
Letter D
Y
Letter Z