def multi_group_by_as_map(self, key=None): from .map import Map key_to_grp = defaultdict(list) for elem in self: for k in key(elem): key_to_grp[k].append(elem) return Map(key_to_grp)
def group_by_as_map(self, key_func=None): '''Group values in to a Map by the value of key function evaluation result. Comparing to ``group_by_as_stream``, there're some pros and cons. Pros: * Elements don't need to be sorted by the key function first. You can call ``map_group_by`` anytime and correct grouping result. Cons: * Key function has to be evaluated to a hashable value. * Not Lazy-evaluating. Consume more memory while grouping. Yield a group as soon as possible. >>> Stream.range(10).group_by_as_map(key_func=lambda n: n % 3) Map({0: Array([0, 3, 6, 9]), 1: Array([1, 4, 7]), 2: Array([2, 5, 8])}) ''' from .map import Map key_to_grp = defaultdict(Array) for elem in self: key_to_grp[key_func(elem)].append(elem) return Map(key_to_grp)
def value_counts(self): '''Get a Counter instance of elements counts Returns ------- Map[E, int] ''' from carriage import Map return Map(Counter(self))
def to_map(self): '''Convert to a Map >>> Stream.range(5, 10, 2).zip_index().to_map() Map({5: 0, 7: 1, 9: 2}) Returns ------- Map ''' from .map import Map return Map(self)