Python 中的嵌套 for 循环与 map 函数的比较

发布于 2024-12-03 13:48:03 字数 259 浏览 0 评论 0原文

我正在 python 中工作,目前有以下代码:

list = []
for a in range(100):
    for b in range(100):
        for c in range(100):
            list.append(run(a,b,c))

其中 run(a,b,c) 返回一个整数(例如,它可以将三个数字相乘)。有没有更快的方法来循环这些数字或使用地图函数?

谢谢 :)

I'm working in python and currently have the following code:

list = []
for a in range(100):
    for b in range(100):
        for c in range(100):
            list.append(run(a,b,c))

where run(a,b,c) returns an integer (for example, it could multiply the three numbers together). Is there a faster way to either loop over these numbers or use a map function?

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

若有似无的小暗淡 2024-12-10 13:48:03

看看itertools-module,特别是product 方法

示例用法:

for i in itertools.product(range(0,100), repeat=3):
    #do stuff with i
    list.append(run(i[0],i[1],i[2]))

函数调用可以缩短为:

list.append(run(*i))

请注意,示例中的 多于。有关解包参数列表的说明,请参阅 docs.python.org

例如,product(range(0,2), Repeat=3)) 的输出如下所示:

(0, 0, 0)
(0, 0, 1)
(0, 1, 0)
(0, 1, 1)
(1, 0, 0)
(1, 0, 1)
(1, 1, 0)
(1, 1, 1)

Have a look at the itertools-module and particulary the product method

example usage:

for i in itertools.product(range(0,100), repeat=3):
    #do stuff with i
    list.append(run(i[0],i[1],i[2]))

Note that the function call can be shortened to:

list.append(run(*i))

in the example above. see docs.python.org for explanation of Unpacking Argument Lists.

As an example, the output from product(range(0,2), repeat=3)) looks like this:

(0, 0, 0)
(0, 0, 1)
(0, 1, 0)
(0, 1, 1)
(1, 0, 0)
(1, 0, 1)
(1, 1, 0)
(1, 1, 1)
腹黑女流氓 2024-12-10 13:48:03

我认为你可以使用 imap 来执行此操作:

from itertools import imap
result = list(imap(run, range(100), range(100), range(100)))

imap 产生其结果...所以如果你想迭代结果,请不要使用 list()

I think you can use imap to do this :

from itertools import imap
result = list(imap(run, range(100), range(100), range(100)))

imap yields its result... so if you want to iterate of the results don't use the list()

假面具 2024-12-10 13:48:03
from itertools import product
my_list = [run(a, b, c) for a, b, c in product(xrange(100), xrange(100), xrange(100))]

或者:

from itertools import product
my_list = [run(a, b, c) for a, b, c in product(xrange(100), repeat=3)]
from itertools import product
my_list = [run(a, b, c) for a, b, c in product(xrange(100), xrange(100), xrange(100))]

Or:

from itertools import product
my_list = [run(a, b, c) for a, b, c in product(xrange(100), repeat=3)]
新雨望断虹 2024-12-10 13:48:03
import itertools    
li = [run(*triple) for triple in itertools.product(xrange(100), repeat=3)]
import itertools    
li = [run(*triple) for triple in itertools.product(xrange(100), repeat=3)]
一抹苦笑 2024-12-10 13:48:03

另一种选择,具体取决于您想要做什么:

my_array = numpy.tile(numpy.arange(100),(3,1)).T
def run(row):
    return numpy.prod(row, 0)
[run(row) for row in my_array]

当然,这一切都取决于 run,例如,如果您想获取产品,您也可以对整个数组进行操作,这要快得多:

my_product = run(my_array.T)
my_product = numpy.prod(my_array, 1)

an alternative, depending on what you want to do exactly:

my_array = numpy.tile(numpy.arange(100),(3,1)).T
def run(row):
    return numpy.prod(row, 0)
[run(row) for row in my_array]

Of course, it all depends on run, e.g. if you want to take the product, you could also operate on the whole array, which is much faster:

my_product = run(my_array.T)
my_product = numpy.prod(my_array, 1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文