0

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.

Eric Snyder
  • 117
  • 3
  • Please scroll down in the first duplicate link, [How can I verify if one list is a subset of another?](https://stackoverflow.com/questions/16579085/how-can-i-verify-if-one-list-is-a-subset-of-another). Some, but not all answers cover the case of conserving duplicates. – Karl Knechtel Sep 18 '22 at 08:36
  • The two answers in the first link that covered duplicates had (a) code that didn't work and (b) a score of -1 (now it has a score of 0). The second link, alas, had not turned up in searches, thanks for having the duplicate to hand. – Eric Snyder Sep 18 '22 at 08:48
  • I didn't; I just knew what to search for (with an external search engine) because I knew how to solve the problem (using `collections.Counter`). – Karl Knechtel Sep 18 '22 at 08:51

0 Answers0