hbutils.collection.sequence¶
unique¶
-
hbutils.collection.sequence.
unique
(s: Sequence[_ElementType]) → Sequence[_ElementType][source]¶ - Overview:
Unique all the values in the given
s
, preserving its original order.
- Parameters:
s – Original sequence.
- Returns:
Unique sequence, with the original type.
- Examples::
>>> from hbutils.collection import unique >>> >>> unique([1, 2, 3, 1]) [1, 2, 3] >>> unique(('a', 'b', 'a', 'c', 'd', 'e', 'b')) ('a', 'b', 'c', 'd', 'e') >>> unique([3, 1, 2, 1, 4, 3]) [3, 1, 2, 4]
group_by¶
-
hbutils.collection.sequence.
group_by
(s: Iterable[_ElementType], key: Callable[[_ElementType], _GroupType], gfunc: Optional[Callable[[List[_ElementType]], _ResultType]] = None) → Dict[_GroupType, _ResultType][source]¶ - Overview:
Divide the elements into groups.
- Parameters:
s – Elements.
key – Group key, should be a callable object.
gfunc – Post-process function for groups, should be a callable object. Default is
None
which means no post-processing will be performed.
- Returns:
Grouping result.
- Examples::
>>> from hbutils.collection import group_by >>> >>> foods = [ ... 'apple', 'orange', 'pear', ... 'banana', 'fish', 'pork', 'milk', ... ] >>> group_by(foods, len) # group by length {5: ['apple'], 6: ['orange', 'banana'], 4: ['pear', 'fish', 'pork', 'milk']} >>> group_by(foods, len, len) # group and get length {5: 1, 6: 2, 4: 4} >>> group_by(foods, lambda x: x[0]) # group by first letter {'a': ['apple'], 'o': ['orange'], 'p': ['pear', 'pork'], 'b': ['banana'], 'f': ['fish'], 'm': ['milk']} >>> group_by(foods, lambda x: x[0], len) # group and get length {'a': 1, 'o': 1, 'p': 2, 'b': 1, 'f': 1, 'm': 1}