迭代一定数量元素的和

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
https://travis-ci.org/shlomif/sum_walker.svg?branch=master

目的

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}$

注释

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
我可以用C++代码使用java代码吗?   java使用JSR303在派生类中提供更具体的约束   java在这个查找唯一路径数算法中我做错了什么?   java如何为2个不同的服务提供商使用2个不同的SSL证书?   java在Gridview上绘制文本   java使用连接for循环构建字符串名   java StringBuilder拆分无法处理某些文件   java事件关注EditText   Java Web Start“找不到URL的缓存资源”   java程序从命令行运行的速度比在Eclipse中慢   java为什么HttpServletRequest会截断#字符上的url输入?   java自定义折叠工具栏平滑标题大小调整   使用Mockito对安卓 java中调用另一个静态函数的函数进行单元测试   http在java客户机中使用cachecontrol头   java如何使用。是否使用Delimiter从输入文件中排除标点符号和数字?   使用上下文作为参数/参数的java   java更有效地从Jar中提取文件   java为多个JButton提供相同的actionListener