Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

python - How to check all items in loop query and continue only if all match the desired result?

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:

That's my code.

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.

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

If that makes sense.

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

EDITED

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))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]].

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

Before:

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

After:

(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)
...

UPDATE

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.)

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))

The output of above code is:

(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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...