How to print the results of successive conditions so that they follow each other and do not sort them by values?

Viewed 42

My code assigns values 1 and 0 in the loop. In the if conditions, it then lists only those where the value 1 is in certain positions. It works as it should.

import os
import numpy as np
from itertools import combinations


a=np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
b=np.array([0, 1, 2, 0, 1, 2, 0, 1, 2])
z1=np.array([1, 1])
z2=np.array([1, 1])
comb_x=np.array([0, 0, 1, 1])
comb_y=np.array([0, 1, 0, 1])
for (j), (k) in zip(a,b):
    #print(j,k)    
    z1[:]=0
    z1[:j]=1
    x12=z1
    z2[:]=0
    z2[:k]=1
    y12=z2  
    #print(x12,y12)
                
    if x12[0]==1 and y12[0]==1:
        print(x12,y12)
    if x12[0]==1 and y12[1]==1:
        print(x12,y12)
    if x12[1]==1 and y12[0]==1:
        print(x12,y12)
    if x12[1]==1 and y12[1]==1:
        print(x12,y12)

The problem is that it does not list them as the individual conditions follow each other in iterations, but it sorts the results of the conditions according to the values regardless of which condition ever went.

my output:

[1 0] [1 0]
[1 0] [1 1]
[1 0] [1 1]
[1 1] [1 0]
[1 1] [1 0]
[1 1] [1 1]
[1 1] [1 1]
[1 1] [1 1]
[1 1] [1 1]

When I run the individual conditions one by one, the list of each condition is as follows

if x12[0]==1 and y12[0]==1:
        print(x12,y12)


[1 0] [1 0]
[1 0] [1 1]
[1 1] [1 0]
[1 1] [1 1]

 if x12[0]==1 and y12[1]==1:
        print(x12,y12)

[1 0] [1 1]
[1 1] [1 1]

if x12[1]==1 and y12[0]==1:
        print(x12,y12)
[1 1] [1 0]
[1 1] [1 1]

if x12[1]==1 and y12[1]==1:
        print(x12,y12)

[1 1] [1 1]

This is what it should look like even if I run them all at once. These are the associated individual conditions above.

required output

[1 0] [1 0]
[1 0] [1 1]
[1 1] [1 0]
[1 1] [1 1]
[1 0] [1 1]
[1 1] [1 1]
[1 1] [1 0]
[1 1] [1 1]
[1 1] [1 1]

I also tried to automate it via for loop and it will sort it as well.

for (h),(n),(r) in zip(comb_x,comb_y,np.arange(0,4)):
        #print(h,n,'iteracia = ',r)
        if x12[h]==1 and y12[n]==1:
            print('pravda',x12,y12)

I don't understand why it sorts me in ascending order by number 1.

Can anyone advise me to do it as the conditions go by and not sort them out?

1 Answers

Your problem is that you print the results as you go and that will mix the result up. You need to save the answer from your first, seconde, third and fourth "if condition" and print them later using a nested loop.

import os
import numpy as np
from itertools import combinations

a = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
b = np.array([0, 1, 2, 0, 1, 2, 0, 1, 2])
z1 = np.array([1, 1])
z2 = np.array([1, 1])
comb_x = np.array([0, 0, 1, 1])
comb_y = np.array([0, 1, 0, 1])
letsgo = [[],[],[],[]]
for (j), (k) in zip(a, b):
    found = False
    # print(j,k)
    z1[:] = 0
    z1[:j] = 1
    x12 = z1
    z2[:] = 0
    z2[:k] = 1
    y12 = z2
    # print(x12,y12)
    if x12[0] == 1 and y12[0] == 1:
        letsgo[0].append(f'{x12} {y12}')
    if x12[0] == 1 and y12[1] == 1:
        letsgo[1].append(f'{x12} {y12}')
    if x12[1] == 1 and y12[0] == 1:
        letsgo[2].append(f'{x12} {y12}')
    if x12[1] == 1 and y12[1] == 1:
        letsgo[3].append(f'{x12} {y12}')
for list in letsgo:
    for string in list:
        print(string)
Related