Skip to content

Commit 5ae24be

Browse files
authored
Merge pull request #115 from tushushu/wip-where-method
implement where method.
2 parents 45ca974 + e28bae8 commit 5ae24be

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

tests/test_base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,15 @@ def test_methods_no_arg(
286286
('var', 'float', [1.0, 2.0, 3.0], 1.0, {"ddof": 1}),
287287
('var', 'int', [1, 2, 3, 4], 1.25, {}),
288288
('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'},),
289298
],
290299
)
291300
def test_methods_with_args(

ulist/python/ulist/core.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,30 @@ def var(self, ddof: int = 0) -> float:
422422
numerator = data.sub_scala(mean).pow_scala(2).sum()
423423
denominator = data.size() - ddof
424424
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

Comments
 (0)