如何检查循环查询中的所有项目并仅在全部匹配所需结果时继续? [英] How to check all items in loop query and continue only if all match the desired result?

查看:45
本文介绍了如何检查循环查询中的所有项目并仅在全部匹配所需结果时继续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

faces_all = list(range(1,25)) # Create list numbered 1-24
for x in itertools.combinations((faces_all),3): # check combinations in numbers 2-23
     if faces_all[0] + sum(x) == 50: # if 1 + combination = 50 continue
        for pair in itertools.combinations([faces_all[0],x],2): # HELP
          if pair != 25:

这是我的代码.

我想要做的只是继续如果 faces_all[0]x 的每个组合(应该是 4 个数字,2 个数字的 6 个组合)不等于 25.

What I want to be able to do is ONLY continue if every combination of faces_all[0] and x (should be 4 numbers, 6 combinations of 2 numbers) is NOT equal to 25.

如果即使是 2 个数字的一​​个组合 = 25,也应该迭代 x 的下一个组合...

If even one combination of 2 numbers = 25, the next combination for x should be iterated...

如果有道理.

我是 Python 新手,我以前唯一的经验是非结构化 BASIC,所以请保持温和.

I'm a python novice and my only previous experience is unstructured BASIC, so please be gentle.

已编辑

非常感谢@wovano.很明显,我有很多关于流量控制的知识.这是我根据@wovano 的回答编写的新代码...

Many thanks to @wovano. It is clear I have much to learn about flow control. Here is my new code based on @wovano's answer...

n = 0   
faces_all = list(range(1,25))
for x in itertools.combinations((faces_all[2:24]),3):
     if faces_all[0] + sum(x) == 50:
        side1 = (faces_all[0], x[0], x[1], x[2])
        for pair in combinations(side1, 2):
            if sum(pair) == 25:
                break
        else:
            n += 1
            print (n,":",side1,":",sum(side1))

推荐答案

[faces_all[0],x] 部分创建了一个包含两个元素的列表.第一个元素是 faces_all[0](在这种情况下总是 1),第二个元素是 x,它是 3 个数字本身的元组.在这种情况下,您想要的是一个包含 4 个数字的列表.您可以像这样创建它:[faces_all[0], x[0], x[1], x[2]].

The part [faces_all[0],x] creates a list of two elements. The first element is faces_all[0] (in this case always 1) and the second element is x, which is a tuple of 3 numbers itself. What you want in this case is a list of 4 numbers. You can create it like this: [faces_all[0], x[0], x[1], x[2]].

提示:如果在最后一个 if 语句之前添加 print(repr(pair)),您可以看到第二次调用 itertools.combinations() 的结果.

Tip: if you add print(repr(pair)) before the last if-statement you can see the results of the second call to itertools.combinations().

之前:

(1, (2, 23, 24))
(1, (3, 22, 24))
(1, (4, 21, 24))
...

之后:

(1, 2)
(1, 23)
(1, 24)
(2, 23)
(2, 24)
(23, 24)
(1, 3)
(1, 22)
(1, 24)
(3, 22)
(3, 24)
(22, 24)
(1, 4)
(1, 21)
(1, 24)
(4, 21)
(4, 24)
(21, 24)
...

更新

现在,一旦您有了正确的数字对,您就可以检查数字之和是否为 25,如果是这种情况,则中断 for 循环.(请注意,itertools.combinations() 返回一个生成器,这意味着如果您中断循环,则不会计算"下一对.)

Now, once you have the correct number pairs, you could check if the sum of the numbers is 25 and break the for-loop if this is the case. (Note that itertools.combinations() returns a generator, meaning that the next pairs are not 'calculated' if you break from the loop.)

完整的代码可能是这样的.请注意 for-else 语句,它在大多数语言中都不存在,在这种情况下很方便.当 for 循环正常"完成(即没有中断)时,会到达 else 语句.

The complete code could be something like this. Note the for-else statement, which does not exist in most languages, and is convenient in this case. The else statement is reached when the for-loop is completed 'normally' (i.e. without break).

import itertools

faces_all = list(range(1, 25))  # Create list numbered 1-24
for x in itertools.combinations((faces_all), 3):  # check combinations in numbers 2-23
     if faces_all[0] + sum(x) == 50:  # if 1 + combination = 50 continue
        four_numbers = (faces_all[0], x[0], x[1], x[2])
        for pair in itertools.combinations(four_numbers, 2):
            if sum(pair) == 25:
                break
        else:
            print(repr(four_numbers))

以上代码的输出为:

(1, 4, 22, 23)
(1, 5, 21, 23)
(1, 6, 20, 23)
(1, 6, 21, 22)
(1, 7, 19, 23)
(1, 7, 20, 22)
(1, 8, 18, 23)
(1, 8, 19, 22)
(1, 8, 20, 21)
(1, 9, 17, 23)
(1, 9, 18, 22)
(1, 9, 19, 21)
(1, 10, 16, 23)
(1, 10, 17, 22)
(1, 10, 18, 21)
(1, 10, 19, 20)
(1, 11, 15, 23)
(1, 11, 16, 22)
(1, 11, 17, 21)
(1, 11, 18, 20)
(1, 12, 14, 23)
(1, 12, 15, 22)
(1, 12, 16, 21)
(1, 12, 17, 20)
(1, 12, 18, 19)
(1, 13, 14, 22)
(1, 13, 15, 21)
(1, 13, 16, 20)
(1, 13, 17, 19)
(1, 14, 15, 20)
(1, 14, 16, 19)
(1, 14, 17, 18)
(1, 15, 16, 18)

这是您要寻找的结果吗?

Is this the result you're looking for?

这篇关于如何检查循环查询中的所有项目并仅在全部匹配所需结果时继续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆