Add is_contained_in
This commit is contained in:
20
snippets/is_contained_in.md
Normal file
20
snippets/is_contained_in.md
Normal file
@ -0,0 +1,20 @@
|
||||
---
|
||||
title: is_contained_in
|
||||
tags: list,intermediate
|
||||
---
|
||||
|
||||
Returns `True` if the elements of the first λιστ are contained in the second one regardless of order, `False` otherwise.
|
||||
|
||||
Use `count()` to check if any value in `a` has more occurences than it has in `b`, returing `False` if any such value is found, `True` otherwise.
|
||||
|
||||
```py
|
||||
def is_contained_in(a, b):
|
||||
for v in set(a):
|
||||
if a.count(v) > b.count(v):
|
||||
return False
|
||||
return True
|
||||
```
|
||||
|
||||
```py
|
||||
is_contained_in([1, 4], [2, 4, 1]) # True
|
||||
```
|
||||
Reference in New Issue
Block a user