Python 循环的列表理解

发布于 2024-12-11 07:45:30 字数 647 浏览 0 评论 0原文

我使用了很多 N 维数组,编写这样的缩进代码很痛苦,而且我知道有些代码可以用列表理解和内联语句替换。例如:

for x in (0,1,2,3):
    for y in (0,1,2,3):
        if x < y:
            print (x, y, x*y)

可以替换为:

print [(x, y, x * y) for x in (0,1,2,3) for y in (0,1,2,3) if x < y]

但是我如何更改操作而不是打印来执行其他操作,例如:

total = x+y

所以我想做的是:

[(total+=x+y) for x in (0,1,2,3) for y in (0,1,2,3) if x < y]

但是这不起作用 有没有

一种聪明的方法可以做到这一点而不是:

for x in (0,1,2,3):
        for y in (0,1,2,3):
            if x < y:
                total+=x+y

I use a lot of N dimensional arrays and it gets a pain to have to write such indented code and I know some codes can be replaced with list comprehensions and inline statements. For example:

for x in (0,1,2,3):
    for y in (0,1,2,3):
        if x < y:
            print (x, y, x*y)

can be replaced with:

print [(x, y, x * y) for x in (0,1,2,3) for y in (0,1,2,3) if x < y]

But how could I change the action instead of print to do something else like:

total = x+y

So what I want to do is something like:

[(total+=x+y) for x in (0,1,2,3) for y in (0,1,2,3) if x < y]

However this doesn't work

Is there a smart way to do this rather than:

for x in (0,1,2,3):
        for y in (0,1,2,3):
            if x < y:
                total+=x+y

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

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

发布评论

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

评论(5

金兰素衣 2024-12-18 07:45:31

使用 numpy。这使您可以使用像向量一样相加的数组:

x = numpy.arange(3)
y = numpy.arange(3)
total = x + y

通过修改后的问题,还添加对 sum 的调用

total = numpy.sum(x+y)

Use numpy. This lets you use arrays that add up like vectors:

x = numpy.arange(3)
y = numpy.arange(3)
total = x + y

With the modified question, add a call to sum as well

total = numpy.sum(x+y)
红衣飘飘貌似仙 2024-12-18 07:45:31

归约函数直接将集合项减少为单个项。您可以在此处阅读有关它们的更多信息,但这应该对您有用:

total=reduce(lambda x,y:x+y,range(4))

或者

total=reduce(lambda x,y:x+y,(0,1,2,3))

Reduce function directly reduces collective items to single item. You can read more about them here, but this should work for you:

total=reduce(lambda x,y:x+y,range(4))

or

total=reduce(lambda x,y:x+y,(0,1,2,3))
油饼 2024-12-18 07:45:31

另一种可能性是:

for x,y in ((x,y) for x in (0,1,2,3) for y in (0,1,2,3) if x < y):
  print (x, y, x * y)

通过这种方式,您可以迭代在列表理解中使用的任何内容,而无需实际创建理解列表(如果您明白我的意思;)如果理解列表很大,可能太大以至于饱和甚至不饱和放在内存里,蛮方便的

Another possibility is:

for x,y in ((x,y) for x in (0,1,2,3) for y in (0,1,2,3) if x < y):
  print (x, y, x * y)

In this way you can iterate over anything you'd use in a list comprehension without actually creating the comprehended list (if you get my meaning ;) If comprehended list is big, maybe so big it saturates or even doesn't fit in memory, that's quite handy..

忘年祭陌 2024-12-18 07:45:30

sum 在这里工作:

total = sum(x+y for x in (0,1,2,3) for y in (0,1,2,3) if x < y)

sum works here:

total = sum(x+y for x in (0,1,2,3) for y in (0,1,2,3) if x < y)
梦在深巷 2024-12-18 07:45:30

作为编写 N 层深度循环的替代方法,您可以使用 itertools.product ()

In [1]: import itertools as it

In [2]: for x, y in it.product((0,1,2,3),(0,1,2,3)):
   ...:     if x < y:
   ...:         print x, y, x*y

0 1 0
0 2 0
0 3 0
1 2 2
1 3 3
2 3 6

这自然延伸到 N 维。

As an alternative to writing loops N levels deep, you could use itertools.product():

In [1]: import itertools as it

In [2]: for x, y in it.product((0,1,2,3),(0,1,2,3)):
   ...:     if x < y:
   ...:         print x, y, x*y

0 1 0
0 2 0
0 3 0
1 2 2
1 3 3
2 3 6

This extends naturally to N dimensions.

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