首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >fortran中的combinations_with_replacement函数?

fortran中的combinations_with_replacement函数?
EN

Stack Overflow用户
提问于 2011-10-27 01:27:24
回答 2查看 587关注 0票数 2

我用Python语言编写了一个小模块,用于获取适合y总成本的x产品的所有可能性。该模块运行良好,但速度较慢。计算6个产品到每个产品的30次迭代大约需要6个小时。所以,我在考虑用FORTRAN重写脚本,看看能不能挤出更快的速度。不幸的是,我是第一次接触FORTRAN,现在还不了解大多数的库等等。

在FORTRAN语言中,有没有类似于python的itertools.combinations_with_replacement(pool, r)的模块/函数,或者实现相同功能的模块?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-27 02:38:11

别干那事。当你需要进行算法优化时,你正在尝试进行微优化。这就是为什么我认为should not use the exponential-time solution是基于itertools而不是better, recursive solution的原因。

票数 2
EN

Stack Overflow用户

发布于 2011-10-27 01:39:28

一言以蔽之,itertools documentation对combinations_with_replacement()有一个纯python等效项。它很简洁,翻译成Fortran应该不难

代码语言:javascript
复制
def combinations_with_replacement(iterable, r):
    # combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC
    pool = tuple(iterable)
    n = len(pool)
    if not n and r:
        return
    indices = [0] * r
    yield tuple(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * (r - i)
        yield tuple(pool[i] for i in indices)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7906461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档