We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 45ca974 + e28bae8 commit 5ae24beCopy full SHA for 5ae24be
2 files changed
tests/test_base.py
@@ -286,6 +286,15 @@ def test_methods_no_arg(
286
('var', 'float', [1.0, 2.0, 3.0], 1.0, {"ddof": 1}),
287
('var', 'int', [1, 2, 3, 4], 1.25, {}),
288
('var', 'int', [1, 2, 3], 1.0, {"ddof": 1}),
289
+
290
+ ("where", "bool", [True, True, False, False], [
291
+ False, False], {"fn": lambda x: x == False},), # noqa: E712
292
+ ("where", "float", [1.0, 2.0, 3.0, 4.0], [
293
+ 1.0, 2.0], {"fn": lambda x: x < 3.0},),
294
+ ("where", "int", [1, 2, 3, 4], [
295
+ 3, 4], {"fn": lambda x: x > 2},),
296
+ ("where", "string", ['foo', 'bar', 'baz'], [
297
+ 'foo', 'baz'], {"fn": lambda x: x != 'bar'},),
298
],
299
)
300
def test_methods_with_args(
ulist/python/ulist/core.py
@@ -422,3 +422,30 @@ def var(self, ddof: int = 0) -> float:
422
numerator = data.sub_scala(mean).pow_scala(2).sum()
423
denominator = data.size() - ddof
424
return numerator / denominator
425
426
+ def where(
427
+ self,
428
+ fn: Callable[[UltraFastList], UltraFastList]
429
+ ) -> UltraFastList:
430
+ """According to the function, return a ulist with elements of self
431
+ correspondingly.
432
433
+ Args:
434
+ fn (Callable[[UltraFastList], UltraFastList]):
435
+ Function to process self and return a BooleanList.
436
437
+ Returns:
438
+ UltraFastList: A ulist object.
439
440
+ Examples
441
+ --------
442
+ >>> import ulist as ul
443
+ >>> arr = ul.arange(6)
444
+ >>> arr
445
+ UltraFastList([0, 1, 2, 3, 4, 5])
446
447
+ >>> result = arr.where(lambda x: x > 2)
448
+ >>> result
449
+ UltraFastList([3, 4, 5])
450
+ """
451
+ return self.filter(fn(self))
0 commit comments