def missing2(list1, list2): list1 = sorted(list1) list2 = sorted(list2) missing = [] while list1 and list2: if list1[0] <= list2[0]: x = list1.pop(0) if x < list2[0]: missing.append(x) else: list2.pop(0) return missing + list1 def missing3(a, b): setb = set(b) return [ai for ai in a if ai not in setb] def missing2S(list1, list2): list1 = sorted(list1) list2 = sorted(list2) missing = [] y=0 for x in list1: while list2 and y < x: y = list2.pop(0) if y > x: missing.append(x) return missing from random import randrange for n in range(20): b = [randrange(1, 100) for i in range(5)] a = b + [randrange(1, 100)] m1, m2 = missing3(a, b), missing2S(a, b) if m1 != m2: print(n, m1, m2, sorted(a), sorted(b))
Standard input is empty
1 [94] [] [10, 17, 29, 67, 89, 94] [10, 17, 29, 67, 89] 7 [99] [] [20, 27, 43, 90, 92, 99] [20, 27, 43, 90, 92] 13 [69] [] [6, 20, 23, 45, 50, 69] [6, 20, 23, 45, 50] 16 [90] [] [23, 32, 42, 66, 74, 90] [23, 32, 42, 66, 74]