Python itertools.accumulate() Examples

The following are 30 code examples of itertools.accumulate(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module itertools , or try the search function .
Example #1
Source File: test_mpctools.py    From mpyc with MIT License 6 votes vote down vote up
def test_accumulate(self):
        secint = mpc.SecInt()
        r = range(1, 9)
        x = [secint(i) for i in r]
        for acc in itertools.accumulate, mpyc.mpctools.accumulate:
            self.assertEqual(mpc.run(mpc.output(list(acc(x)))),
                             list(itertools.accumulate(r)))
            self.assertEqual(mpc.run(mpc.output(list(acc(x, mpc.mul)))),
                             list(itertools.accumulate(r, operator.mul)))
            self.assertEqual(mpc.run(mpc.output(list(acc(x, mpc.min)))),
                             list(itertools.accumulate(r, min)))
            self.assertEqual(mpc.run(mpc.output(list(acc(x, mpc.max)))),
                             list(itertools.accumulate(r, max)))
        a = secint(10)
        self.assertEqual(mpc.run(mpc.output(list(acc(itertools.repeat(a, 5), mpc.mul, secint(1))))),
                         [1, 10, 10**2, 10**3, 10**4, 10**5]) 
Example #2
Source File: counting_sort.py    From wangzheng0822-algo with Apache License 2.0 6 votes vote down vote up
def counting_sort(a: List[int]):
    if len(a) <= 1: return
    
    # a中有counts[i]个数不大于i
    counts = [0] * (max(a) + 1)
    for num in a:
        counts[num] += 1
    counts = list(itertools.accumulate(counts))

    # 临时数组,储存排序之后的结果
    a_sorted = [0] * len(a)
    for num in reversed(a):
        index = counts[num] - 1
        a_sorted[index] = num
        counts[num] -= 1
    
    a[:] = a_sorted 
Example #3
Source File: roulette_wheel_selection.py    From gaft with GNU General Public License v3.0 6 votes vote down vote up
def select(self, population, fitness):
        ''' Select a pair of parent using FPS algorithm.

        :param population: Population where the selection operation occurs.
        :type population: :obj:`gaft.components.Population`

        :return: Selected parents (a father and a mother)
        :rtype: list of :obj:`gaft.components.IndividualBase`
        '''
        # Normalize fitness values for all individuals.
        fit = population.all_fits(fitness)
        min_fit = min(fit)
        fit = [(i - min_fit) for i in fit]

        # Create roulette wheel.
        sum_fit = sum(fit)
        wheel = list(accumulate([i/sum_fit for i in fit]))

        # Select a father and a mother.
        father_idx = bisect_right(wheel, random())
        father = population[father_idx]
        mother_idx = (father_idx + 1) % len(wheel)
        mother = population[mother_idx]

        return father, mother 
Example #4
Source File: random.py    From GraphicDesignPatternByPython with MIT License 6 votes vote down vote up
def choices(self, population, weights=None, *, cum_weights=None, k=1):
        """Return a k sized list of population elements chosen with replacement.

        If the relative weights or cumulative weights are not specified,
        the selections are made with equal probability.

        """
        random = self.random
        if cum_weights is None:
            if weights is None:
                _int = int
                total = len(population)
                return [population[_int(random() * total)] for i in range(k)]
            cum_weights = list(_itertools.accumulate(weights))
        elif weights is not None:
            raise TypeError('Cannot specify both weights and cumulative weights')
        if len(cum_weights) != len(population):
            raise ValueError('The number of weights does not match the population')
        bisect = _bisect.bisect
        total = cum_weights[-1]
        return [population[bisect(cum_weights, random() * total)] for i in range(k)]

## -------------------- real-valued distributions  -------------------

## -------------------- uniform distribution ------------------- 
Example #5
Source File: fractional_knapsack.py    From Python with MIT License 6 votes vote down vote up
def fracKnapsack(vl, wt, W, n):
    """
    >>> fracKnapsack([60, 100, 120], [10, 20, 30], 50, 3)
    240.0
    """

    r = list(sorted(zip(vl, wt), key=lambda x: x[0] / x[1], reverse=True))
    vl, wt = [i[0] for i in r], [i[1] for i in r]
    acc = list(accumulate(wt))
    k = bisect(acc, W)
    return (
        0
        if k == 0
        else sum(vl[:k]) + (W - acc[k - 1]) * (vl[k]) / (wt[k])
        if k != n
        else sum(vl[:k])
    ) 
Example #6
Source File: Config.py    From topydo with GNU General Public License v3.0 6 votes vote down vote up
def column_keymap(self):
        """ Returns keymap and keystates used in column mode """
        keystates = set()

        shortcuts = self.cp.items('column_keymap')
        keymap_dict = dict(shortcuts)

        for combo, action in shortcuts:
            # add all possible prefixes to keystates
            combo_as_list = re.split('(<[A-Z].+?>|.)', combo)[1::2]
            if len(combo_as_list) > 1:
                keystates |= set(accumulate(combo_as_list[:-1]))

            if action in ['pri', 'postpone', 'postpone_s']:
                keystates.add(combo)

            if action == 'pri':
                for c in ascii_lowercase:
                    keymap_dict[combo + c] = 'cmd pri {} ' + c

        return (keymap_dict, keystates) 
Example #7
Source File: variation.py    From few with GNU General Public License v3.0 6 votes vote down vote up
def is_valid_program(self,p):
        """checks whether program p makes a syntactically valid tree.

        checks that the accumulated program length is always greater than the
        accumulated arities, indicating that the appropriate number of arguments is
        alway present for functions. It then checks that the sum of arties +1
        exactly equals the length of the stack, indicating that there are no
        missing arguments.
        """
        # print("p:",p)
        arities = list(a.arity[a.in_type] for a in p)
        accu_arities = list(accumulate(arities))
        accu_len = list(np.arange(len(p))+1)
        check = list(a < b for a,b in zip(accu_arities,accu_len))
        # print("accu_arities:",accu_arities)
        # print("accu_len:",accu_len)
        # print("accu_arities < accu_len:",accu_arities<accu_len)
        return all(check) and sum(a.arity[a.in_type] for a in p) +1 == len(p) and len(p)>0 
Example #8
Source File: __init__.py    From picklable-itertools with MIT License 6 votes vote down vote up
def test_accumulate():
    if not six.PY3:
        raise SkipTest()
    yield verify_same, accumulate, itertools.accumulate, None, [5, 4, 9]
    yield verify_same, accumulate, itertools.accumulate, None, ['a', 'b', 'c']
    yield (verify_same, accumulate, itertools.accumulate, None,
           [[1], [2], [3, 4]])
    yield (verify_same, accumulate, itertools.accumulate, None, [9, 1, 2],
           sub)
    yield verify_pickle, accumulate, itertools.accumulate, 3, 1, [5, 4, 9]
    yield verify_pickle, accumulate, itertools.accumulate, 3, 0, [5, 4, 9]
    yield (verify_pickle, accumulate, itertools.accumulate, 3, 2,
           ['a', 'b', 'c'])
    yield (verify_pickle, accumulate, itertools.accumulate, 2, 1,
           ['a', 'b', 'c'])
    yield (verify_pickle, accumulate, itertools.accumulate, 3, 1,
           [[1], [2], [3, 4]])
    yield (verify_pickle, accumulate, itertools.accumulate, 2, 1,
           [9, 1, 2], sub) 
Example #9
Source File: _tksheet.py    From tksheet with MIT License 6 votes vote down vote up
def set_row_heights(self, row_heights = None, canvas_positions = False, reset = False, verify = False):
        if reset:
            self.MT.reset_row_positions()
            return
        if isinstance(row_heights, list):
            qmin = self.MT.min_rh
            if canvas_positions:
                if verify:
                    self.MT.row_positions = list(accumulate(chain([0], (height if qmin < height else qmin
                                                                        for height in [x - z for z, x in zip(islice(row_heights, 0, None),
                                                                                                             islice(row_heights, 1, None))]))))
                else:
                    self.MT.row_positions = row_heights
            else:
                if verify:
                    self.MT.row_positions = [qmin if z < qmin or not isinstance(z, int) or isinstance(z, bool) else z for z in row_heights]
                else:
                    self.MT.row_positions = list(accumulate(chain([0], (height for height in row_heights)))) 
Example #10
Source File: counting_sort.py    From algo with Apache License 2.0 6 votes vote down vote up
def counting_sort(a: List[int]):
    if len(a) <= 1: return
    
    # a中有counts[i]个数不大于i
    counts = [0] * (max(a) + 1)
    for num in a:
        counts[num] += 1
    counts = list(itertools.accumulate(counts))

    # 临时数组,储存排序之后的结果
    a_sorted = [0] * len(a)
    for num in reversed(a):
        index = counts[num] - 1
        a_sorted[index] = num
        counts[num] -= 1
    
    a[:] = a_sorted 
Example #11
Source File: turas.py    From turingas with MIT License 5 votes vote down vote up
def GetParameterConstant(var_name, var_dict, bank, base, offset=0):
  index = var_dict['name_list'].index(var_name) # Use .index() is safe here. Elements are unique.
  prefix_sum = list(accumulate(var_dict['size_list']))
  size = var_dict['size_list'][index]
  
  if size - offset*4 < 0:
    raise Exception(f'Parameter {var_name} is of size {size}. Cannot have offset {offset}.')

  offset = prefix_sum[index] - size + offset * 4# FIXME: Currently we assume elements of all arrays are 4 Bytes in size.
  
  return 'c[0x{:x}]['.format(bank) + '0x{:x}'.format(base + offset) + ']'

# Replace register and parameter. 
Example #12
Source File: tools.py    From repoll with Apache License 2.0 5 votes vote down vote up
def slot_split_part(n):
    """
    根据slot等分的份数,格式化redis命令行添加slot所需的格式
    :param n: split_integer函数的返回结果
    :return:
    """
    return ['..'.join((str(i+1), str(j))) for i, j in zip([-1]+list(it.accumulate(n[:-1])), it.accumulate(n))] 
Example #13
Source File: utils.py    From attn2d with MIT License 5 votes vote down vote up
def get_token_to_word_mapping(tokens, exclude_list):
    n = len(tokens)
    word_start = [int(token not in exclude_list) for token in tokens]
    word_idx = list(accumulate(word_start))
    token_to_word = {i: word_idx[i] for i in range(n)}
    return token_to_word 
Example #14
Source File: fns.py    From GitSavvy with MIT License 5 votes vote down vote up
def accumulate(iterable, initial=None):
    # type: (Iterable[int], int) -> Iterable[int]
    if initial is None:
        return accumulate_(iterable)
    else:
        return accumulate_(chain([initial], iterable)) 
Example #15
Source File: websocket.py    From TwitchIO with MIT License 5 votes vote down vote up
def generate_jitter():
        # Generates a random number between around 1 and 10
        jitter = 0

        while jitter == 11 or jitter == 0:
            bites = secrets.token_bytes(2)
            number = itertools.accumulate(bites)
            jitter = int(sum(number) / 2 ** 6)

        return jitter 
Example #16
Source File: texter.py    From chamanti_ocr with Apache License 2.0 5 votes vote down vote up
def get_next_char(char):
    if char in bi_acc_cache:
        followers, accumulated_counts = bi_acc_cache[char]
    else:
        followers, counts = zip(*bigram_counts[char].items())
        accumulated_counts = tuple(accumulate(counts))
        bi_acc_cache[char] = followers, accumulated_counts

    loc = int(accumulated_counts[-1] * random())
    follower = bisect.bisect(accumulated_counts, loc)
    return followers[follower] 
Example #17
Source File: code_generation_objects.py    From forte with Apache License 2.0 5 votes vote down vote up
def make_module_dirs(self, tempdir: str, destination: str,
                         include_init: bool):
        """
        Create entry sub-directories with .generated file to indicate the
         subdirectory is created by this procedure. No such file will be added
         if the directory already exists.

        Args:
            tempdir: A temp directory to create the structure, code will be
              first generated here.
            destination: The destination directory where the code should be
              placed
            include_init: True if `__init__.py` is to be generated in existing
              packages in which `__init__.py` does not already exists
        Returns:
        """
        entry_dir_split = split_file_path(self.pkg_dir)

        rel_dir_paths = it.accumulate(entry_dir_split, os.path.join)
        for rel_dir_path in rel_dir_paths:
            temp_path = os.path.join(tempdir, rel_dir_path)
            if not os.path.exists(temp_path):
                os.mkdir(temp_path)

            dest_path = os.path.join(destination, rel_dir_path)
            dest_path_exists = os.path.exists(dest_path)
            if not dest_path_exists:
                Path(os.path.join(temp_path, AUTO_GEN_FILENAME)).touch()

            # Create init file
            if not dest_path_exists or include_init:
                init_file_path = os.path.join(temp_path, '__init__.py')
                with open(init_file_path, 'w') as init_file:
                    init_file.write(f'# {AUTO_GEN_SIGNATURE}\n') 
Example #18
Source File: misc.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def accumulate(iterable, func=operator.add):
        it = iter(iterable)
        try:
            total = next(it)
        except StopIteration:
            return
        yield total
        for element in it:
            total = func(total, element)
            yield total

# 2.x/3.x compatibility 
Example #19
Source File: api.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def accumulate(iterable, func=operator.add):
        """Return running totals"""
        # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
        # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
        it = iter(iterable)
        try:
            total = next(it)
        except StopIteration:
            return
        yield total
        for element in it:
            total = func(total, element)
            yield total 
Example #20
Source File: binary_individual.py    From gaft with GNU General Public License v3.0 5 votes vote down vote up
def _get_gene_indices(self):
        '''
        Helper function to get gene slice indices.
        '''
        end_indices = list(accumulate(self.lengths))
        start_indices = [0] + end_indices[: -1]
        return list(zip(start_indices, end_indices)) 
Example #21
Source File: _tksheet_column_headers.py    From tksheet with MIT License 5 votes vote down vote up
def set_width_of_all_cols(self, width = None, only_set_if_too_small = False, recreate = True):
        if width is None:
            if self.MT.all_columns_displayed:
                iterable = range(self.MT.total_data_cols())
            else:
                iterable = range(len(self.MT.displayed_columns))
            self.MT.col_positions = list(accumulate(chain([0], (self.set_col_width(cn, only_set_if_too_small = only_set_if_too_small, recreate = False, return_new_width = True) for cn in iterable))))
        elif width is not None:
            if self.MT.all_columns_displayed:
                self.MT.col_positions = list(accumulate(chain([0], (width for cn in range(self.MT.total_data_cols())))))
            else:
                self.MT.col_positions = list(accumulate(chain([0], (width for cn in range(len(self.MT.displayed_columns))))))
        if recreate:
            self.MT.recreate_all_selection_boxes()
            self.MT.resize_dropdowns() 
Example #22
Source File: linear_ranking_selection.py    From gaft with GNU General Public License v3.0 5 votes vote down vote up
def select(self, population, fitness):
        ''' Select a pair of parent individuals using linear ranking method.

        :param population: Population where the selection operation occurs.
        :type population: :obj:`gaft.components.Population`

        :return: Selected parents (a father and a mother)
        :rtype: list of :obj:`gaft.components.IndividualBase`
        '''
        # Individual number.
        NP = len(population)

        # Add rank to all individuals in population.
        all_fits = population.all_fits(fitness)
        indvs = population.individuals
        sorted_indvs = sorted(indvs,
                              key=lambda indv: all_fits[indvs.index(indv)])

        # Assign selection probabilities linearly.
        # NOTE: Here the rank i belongs to {1, ..., N}
        p = lambda i: (self.pmin + (self.pmax - self.pmin)*(i-1)/(NP-1))
        probabilities = [self.pmin] + [p(i) for i in range(2, NP)] + [self.pmax]

        # Normalize probabilities.
        psum = sum(probabilities)
        wheel = list(accumulate([p/psum for p in probabilities]))

        # Select parents.
        father_idx = bisect_right(wheel, random())
        father = sorted_indvs[father_idx]
        mother_idx = (father_idx + 1) % len(wheel)
        mother = sorted_indvs[mother_idx]

        return father, mother 
Example #23
Source File: exponential_ranking_selection.py    From gaft with GNU General Public License v3.0 5 votes vote down vote up
def select(self, population, fitness):
        ''' Select a pair of parent individuals using exponential ranking method.

        :param population: Population where the selection operation occurs.
        :type population: :obj:`gaft.components.Population`

        :return: Selected parents (a father and a mother)
        :rtype: list of :obj:`gaft.components.IndividualBase`
        '''
        # Individual number.
        NP = len(population)

        # Add rank to all individuals in population.
        all_fits = population.all_fits(fitness)
        indvs = population.individuals
        sorted_indvs = sorted(indvs,
                              key=lambda indv: all_fits[indvs.index(indv)])

        # NOTE: Here the rank i belongs to {1, ..., N}
        p = lambda i: self.base**(NP - i)
        probabilities = [p(i) for i in range(1, NP + 1)]

        # Normalize probabilities.
        psum = sum(probabilities)
        wheel = list(accumulate([p/psum for p in probabilities]))

        # Select parents.
        father_idx = bisect_right(wheel, random())
        father = sorted_indvs[father_idx]
        mother_idx = (father_idx + 1) % len(wheel)
        mother = sorted_indvs[mother_idx]

        return father, mother 
Example #24
Source File: sentencepiece_to_word_alignments.py    From alignment-scripts with MIT License 5 votes vote down vote up
def get_mapping(file_path):
    with open(file_path, "r", encoding="utf-8") as f:
        for l in f:
            subwords = l.strip().split()  
            yield list(itertools.accumulate([int('▁' in x) for x in subwords])) 
Example #25
Source File: misc.py    From dynetx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def accumulate(iterable, func=operator.add):
        it = iter(iterable)
        try:
            total = next(it)
        except StopIteration:
            return
        yield total
        for element in it:
            total = func(total, element)
            yield total 
Example #26
Source File: interpreter.py    From jellylanguage with MIT License 5 votes vote down vote up
def reduce_cumulative(links, outmost_links, index):
	ret = [attrdict(arity = 1)]
	if len(links) == 1:
		ret[0].call = lambda t: list(itertools.accumulate(iterable(t), lambda x, y: dyadic_link(links[0], (x, y))))
	else:
		ret[0].call = lambda z: [reduce_simple(t, links[0]) for t in split_rolling(iterable(z), links[1].call())]
	return ret 
Example #27
Source File: search_controller.py    From cccatalog-api with MIT License 5 votes vote down vote up
def _paginate_with_dead_link_mask(s: Search, page_size: int,
                                  page: int) -> Tuple[int, int]:
    """
    Given a query, a page and page_size, return the start and end
    of the slice of results.

    :param s: The elasticsearch Search object
    :param page_size: How big the page should be.
    :param page: The page number.
    :return: Tuple of start and end.
    """
    query_hash = get_query_hash(s)
    query_mask = get_query_mask(query_hash)
    if not query_mask:
        start = 0
        end = ceil(page_size * page / (1 - DEAD_LINK_RATIO))
    elif page_size * (page - 1) > sum(query_mask):
        start = len(query_mask)
        end = ceil(page_size * page / (1 - DEAD_LINK_RATIO))
    else:
        accu_query_mask = list(accumulate(query_mask))
        start = 0
        if page > 1:
            try:
                start = accu_query_mask.index(page_size * (page - 1) + 1)
            except ValueError:
                start = accu_query_mask.index(page_size * (page - 1)) + 1
        if page_size * page > sum(query_mask):
            end = ceil(page_size * page / (1 - DEAD_LINK_RATIO))
        else:
            end = accu_query_mask.index(page_size * page) + 1
    return start, end 
Example #28
Source File: vi.py    From android_universal with MIT License 5 votes vote down vote up
def accumulate(iterable):
        " Super simpel 'accumulate' implementation. "
        total = 0
        for item in iterable:
            total += item
            yield total 
Example #29
Source File: core.py    From lineflow with MIT License 5 votes vote down vote up
def _get_lengths(self) -> List[int]:
        return list(accumulate(len(d) for d in self._datasets)) 
Example #30
Source File: network.py    From simchain with The Unlicense 5 votes vote down vote up
def _accumulate(l):
    return list(accumulate(l))
    
#random data
# =============================================================================