迭代一定数量元素的和
sum-walker的Python项目详细描述
Info: | This is the README file for Sum walker. |
---|---|
Author: | Shlomi Fish <shlomif@cpan.org> |
Copyright: | © 2020, Shlomi Fish. |
Date: | 2020-02-25 |
Version: | 0.8.2 |
目的
sum_-walker-PyPI分布允许迭代递增 某个元素流中某个数量(如:2或3)的和 增加整数。在
安装
pip3安装sum_walker
使用
打印两个整数和的简单示例:
#! /usr/bin/env python3 # -*- coding: utf-8 -*- # vim:fenc=utf-8 # # Copyright © 2020 Shlomi Fish <shlomif@cpan.org> # # Distributed under the terms of the MIT license. # # This program displays increasing sums of two positive integers from six import print_ import sum_walker.iterator_wrapper def main(): def natural_nums_iter(): ret = 1 while True: yield ret ret += 1 walker = sum_walker.iterator_wrapper.Walker( counts=[2], iterator=natural_nums_iter()) def print_next(): nonlocal walker sum_, coords = next(walker) print_("{} = {}".format( sum_, " ; ".join( [" + ".join([str(x.value) for x in permutation]) for permutation in coords]))) # Prints «2 = 1 + 1» print_next() # Prints «3 = 1 + 2» print_next() # Prints «4 = 1 + 3 ; 2 + 2» print_next() # Prints «5 = 1 + 4 ; 2 + 3» print_next() # Prints «6 = 1 + 5 ; 2 + 4 ; 3 + 3» print_next() main()
一个更有趣的例子,这次使用的是更漂亮的 sum\u walker模块:
^{pr2}$注释
版权所有
版权所有©2020,Shlomi Fish。 版权所有。在
以源代码和二进制形式重新分发和使用,有无 允许修改,前提是下列条件 会议:
- 源代码的再分配必须保留上述版权 注意,此条件列表和以下免责声明。在
- 二进制形式的再分配必须复制上述版权 免责声明及以下条件 分发时提供的文件和/或其他材料。在
- 既没有这个软件的作者的名字也没有 本软件的贡献者可能被用来认可或推广 从本软件衍生而来的产品,未经事先书面说明 同意。在
本软件由版权所有人和贡献者提供 “原样”和任何明示或暗示的保证,包括但不 仅限于对适销性和适用性的默示保证 一个特殊的目的被否认。在任何情况下,版权 所有人或出资人对任何直接的、间接的、偶然的, 特殊、惩戒性或后果性损害(包括但不包括 仅限于采购替代货物或服务;丧失使用, 数据或利润;或业务中断) 责任理论,无论是合同、严格责任还是侵权 (包括疏忽或其他原因)在使用过程中产生的 即使被告知有这种损坏的可能性。在
- 项目
标签: