difference_with()
Creates an array of values not included in the other given arrays using `comparator` for equality comparisons.
Implementation
Args: array (List[T]): The array to inspect. values (List[T]): The values to exclude. comparator (Optional[Callable[[T, T], bool]]): The comparator to transform values. Returns: List[T]: Returns the new array of values not included in the other given arrays.
Example
difference_with([2.1, 1.2, 2.3], [2.3, 3.4], lambda a, b: round(a, 1) == round(b, 1))
Expected output: [1.2]
Source Code
def difference_with(array: List[T], *args) -> List[T]:
if not args:
return list(array)
comparator = args[-1] if callable(args[-1]) else None # type: ignore
values = args[0] if comparator else args[0]
if comparator is None:
def comparator(a, b):
return a == b # type: ignore
result: List[T] = []
for item in array:
if not any(comparator(item, v) for v in values):
result.append(item)
return result