本文介绍了如何用Numpy数组替换For循环和IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的numpy数组:

I have a numpy array like this:

[[1, 2], [1, 3], [2, 1], [2, 2], [2, 3], ...]

我想得到所有子"数组(即[X,Y])三乘三的组合:

I would like to get the combinations of all "sub" arrays (i.e [X, Y]) three by three:

[[1, 1] [1, 1] [1, 1],
 [1, 1] [1, 1] [1, 2],
 [1, 1] [1, 1] [1, 3],
 ...
 [5, 5] [5, 5], [5, 4],
 [5, 5] [5, 5], [5, 5]]

然后,我需要对每种组合应用条件:

Then, I need to apply conditions on each combinations:

  • X1, X2, X3 > 0
  • X1+Y1 <= X2
  • X2+Y2 <= X3
  • [X1, Y1] =! [X2, Y2]
  • [X2, Y2] =! [X3, Y3]
  • ...
  • X1, X2, X3 > 0
  • X1+Y1 <= X2
  • X2+Y2 <= X3
  • [X1, Y1] =! [X2, Y2]
  • [X2, Y2] =! [X3, Y3]
  • ...

由于组合数量众多,我绝对需要避免for循环.

I absolutely need to avoid for loops because of the high number of combinations.

有什么想法如何在有效的执行时间内完成这项工作吗?

Any idea how to get this done in an effective time of execution?

我当前的for循环和if语句代码:

My current code with for loops and if statements:

组合= []留在我的列表中:

Combination = []for left in mylist:

    if left[0] > 0:

        for center in mylist:

            if (center[0] > 0
                and center[0] >= left[0] + left[1]
                and center[1] / left[1] < 2
                and center[0] / left[0] < 2
                and left[1] / center[1] < 2
                and left[0] / center[1] < 2
                and str(left[0]) + "y" + str(left[1]) + "y" != str(center[0]) + "y" + str(center[1]) + "y"
                ):

                for right in mylist:

                    if (right[0] > 0
                        and right[0] >= center[0] + center[1]
                        and right[1] / center[1] < 2
                        and right[0] / center[0] < 2
                        and center[1] / right[1] < 2
                        and center[0] / right[0] < 2
                        and str(right[0]) + "y" + str(right[1]) + "y" != str(center[0]) + "y" + str(center[1]) + "y"
                        ):

                        Combination.append([[left[0], left[1]], [center[0], center[1]], [right[0], right[1]])

推荐答案

编辑:您甚至不需要itertools即可使用numpy创建组合,而且速度非常快

You don't even need itertools you can use numpy to create the combinations and it's extremely fast

# This is your input array from [1,1] to [5,5]
a = np.array(np.meshgrid(np.arange(1,6), np.arange(1,6))).T.reshape(-1,2)

b = np.array(np.meshgrid(a, a, a)).T.reshape(-1, 3, 2)

如您所见,它需要6毫秒:5.88 ms ± 836 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

As you can see it takes 6ms: 5.88 ms ± 836 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

您的数组现在看起来像这样:

Your array looks like this now:


array([[[1, 1],
        [1, 1],
        [1, 1]],

       [[1, 1],
        [1, 1],
        [1, 2]],

       [[1, 1],
        [1, 1],
        [1, 3]],

       ...,

       [[5, 5],
        [5, 5],
        [5, 3]],

       [[5, 5],
        [5, 5],
        [5, 4]],

       [[5, 5],
        [5, 5],
        [5, 5]]])

由于这是一个numpy数组,因此可以安全地使用for循环检查条件.例如,row[0,0]将是您的X1,而row[0,1]将是您的Y1等.

Because this is a numpy array now, you can safely use a for loop to check your conditions. For example row[0,0] would be your X1 and row[0,1] would be your Y1 etc.

for row in b:
    row[0,0] + row[0,1] <= row[1,0]

这也需要很短的时间来执行:10.3 ms ± 278 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

This also takes a really short amount of time to execute: 10.3 ms ± 278 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

因此,您也可以在其他条件下安全地执行此操作.

So you can safely do this for your other conditions aswell.

这篇关于如何用Numpy数组替换For循环和IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:00