Looking for a function similar to is_subset()
but for lists, and the key is that duplicates should be counted but not overcounted. Here are examples to show what I want. The function would take MainList
and TestList
as arguments.
MainList = [1, 2, 5, 3, 2, 7, 3, 3, 8, 1]
TestList = [1, 2, 3] # returns True
TestList = [3, 3, 3, 1] # returns True
TestList = [1, 3, 4, 5] # returns False: 4 is not in the main list
TestList = [1, 2, 1, 2, 2, 3] # returns False: this list has too many 2s
TestList = [5, 3, 1, 8] # returns True: order need not be conserved
TestList = [5, 5, 5, 5] # returns False: Constructs using all() return True
This seems to be to be the obvious meaning of sublist, but it doesn't seem to be?
We don't have sets--duplicates need to be conserved--so we can't use set intersections or similar methods. Just using is in
looks at single elements, I believe. Constructions found elsewhere using all()
will return True if the potential sublist has too many duplicates.
I've looked through multiple questions and their answers. Search is annoyingly difficult because "sublist" can also mean an element of a list of lists, and "duplicate" is pretty frequently used in other ways on SE/SO.
Any thoughts or help? Hoping there's a way to do this without comparing element by element.