我有两个列表,我想比较和打印出两者的共同点
things=['Apple', 'Orange', 'Cherry','banana','dog','door','Chair']
otherThings=['Apple', 'Orange','TV' ,'Cherry','banana','Cat','Pen','Computer','Book']
if (things == otherThings): # this condtion will not work
print "%s\t%s" % (things, otherThings)
else:
print "None"
问题:在这种情况下,我应该使用什么合适的条件
预期结果:['Apple', 'Orange','Cherry','banana']
将它们转换成sets instead,然后得到两者的交集
代码段:
列表理解将为您的“预期结果”建立列表:
改为打印:
哪个更像
一种方法是使用
set
和逻辑and
:相关问题 更多 >
编程相关推荐