Python compare two lists return True or False
Compare two lists and return BooleanI would use MapSet.subset?/2 here: iex(1)> list1 = [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7] iex(2)> list2 = [3, 4, 5] [3, 4, 5] iex(3)> MapSet.new(list2) |> MapSet.subset?(MapSet.new(list1)) true iex(4)> list2 = [3, 4, 5, 9] [3, 4, 5, 9] iex(5)> MapSet.new(list2) |> MapSet.subset?(MapSet.new(list1)) falseThis will run in O(n log n) time as opposed to Kernel.--/2 which runs in O(n^2). (This won't make a significant difference for small lists though.) Check if length(list2 -- list1) is equal to zero You can use MapSet.equal?: iex(1)> MapSet.equal?(MapSet.new(list1), MapSet.new(list2)) true iex(2)> list1 = [1,2,3,4,5,6,7] [1, 2, 3, 4, 5, 6, 7] iex(3)> list2 = [3,4,5] [3, 4, 5] iex(4)> MapSet.equal?(MapSet.new(list1), MapSet.new(list2)) falseComments
Comments are closed. |