>>> lst1=['a','b','d','c','b','c','a','a']
>>> lst2=['a','b','e','c','f','f','g','a']
方法一:转为集合,用集合运算得到结果
>>> set(lst1)&set(lst2) {'a', 'c', 'b'} >>> set(lst1)^set(lst2) {'d', 'g', 'e', 'f'} >>>
方法二:列表推导式
>>> set([i for i in lst1 if i in lst2]+[i for i in lst2 if i in lst1]) {'c', 'b', 'a'} >>> set([i for i in lst1 if i not in lst2]+[i for i in lst2 if i not in lst1]) {'d', 'f', 'e', 'g'} >>>
方法三:用代码写原始的循环遍历(略)
拿分走人......