Python Forum
First for loop stops after first iteration - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: First for loop stops after first iteration (/thread-17943.html)

Pages: 1 2


First for loop stops after first iteration - Divanova94 - Apr-30-2019

I am creating a matrix array by reading two different lists with for loop. But the second for loop stops after the first iteration.
The print output is only with one parameter *1 instead of the all other that are in the lists in the list f1.


import numpy as np
f1 = [['N1M2', 'N1M3', 'N1M4', 'N1M5'], ['M1N2', 'N2M3', 'N2M4', 'N2M5'], ['N1M3', 'N1M4', 'N1M5', 'N2M3', 'N2M4', 'N2M5'], ['M1N3', 'M2N3', 'N3M4', 'N3M5'], ['N1M2', 'N1M4', 'N1M5', 'M2N3', 'N3M4', 'N3M5'], ['M1N2', 'M1N3', 'N2M4', 'N2M5', 'N3M4', 'N3M5'], ['N1M4', 'N1M5', 'N2M4', 'N2M5', 'N3M4', 'N3M5'], ['M1N4', 'M2N4', 'M3N4', 'N4M5'], ['N1M2', 'N1M3', 'N1M5', 'M2N4', 'M3N4', 'N4M5'], ['M1N2', 'M1N4', 'N2M3', 'N2M5', 'M3N4', 'N4M5'], ['N1M3', 'N1M5', 'N2M3', 'N2M5', 'M3N4', 'N4M5'], ['M1N3', 'M1N4', 'M2N3', 'M2N4', 'N3M5', 'N4M5'], ['N1M2', 'N1M5', 'M2N3', 'M2N4', 'N3M5', 'N4M5'], ['M1N2', 'M1N2', 'M1N3', 'N2M5', 'N3M5', 'N4M5'], ['N1M5', 'N2M5', 'N3M5', 'N4M5'], ['M1N5', 'M2N5', 'M3N5', 'M4N5'], ['N1M2', 'N1M3', 'N1M4', 'M2N5', 'M3N5', 'M4N5'], ['M1N2', 'M1N5', 'N2M3', 'N2M4', 'M3N5', 'M4N5'], ['N1M3', 'N1M4', 'N2M3', 'N2M4', 'M3N5', 'M4N5'], ['M1N3', 'M1N5', 'M2N3', 'M2N5', 'N3M4', 'M4N5'], ['N1M2', 'N1M4', 'M2N3', 'M2N5', 'N3M4', 'M4N5'], ['M1N2', 'M1N3', 'M1N5', 'N2M4', 'N3M4', 'M4N5'], ['N1M4', 'N2M4', 'N3M4', 'M4N5'], ['M1N4', 'M1N5', 'M2N4', 'M2N5', 'M3N4', 'M3N5'], ['N1M2', 'N1M3', 'M2N4', 'M2N5', 'M3N4', 'M3N5'], ['M1N2', 'M1N4', 'M1N5', 'N2M3', 'M3N4', 'M3N5'], ['N1M3', 'N2M3', 'M3N4', 'M3N5'], ['M1N3', 'M1N4', 'M1N5', 'M2N3', 'M2N4', 'M2N5'], ['N1M2', 'M2N3', 'M2N4', 'M2N5'], ['M1N2', 'M1N3', 'M1N4', 'M1N5']]

f2 = ['M1N2','M1N3','M1N4','M1N5','M2N3','M2N4','M2N5','M3N4','M3N5','M4N5','N1M2','N1M3','N1M4','N1M5','N2M3','N2M4','N2M5','N3M4','N3M5','N4M5']

def get_matrix(f1, f2):
    new = []
    for j in f1:
        for x in j:
            for i in f2:
                if i == x:
                    new.append('%s*1' % i)
                        #new.append('0')
                else:
                    new.append('%s*0' % i)
                        #new.append('1')
            break
    new1 = np.array(new)
    shape = ( 30, 20 )
    new1.reshape( shape )
    return new1.reshape( shape )

print (get_matrix(f1, f2))
Output:
[['M1N2*0' 'M1N3*0' 'M1N4*0' 'M1N5*0' 'M2N3*0' 'M2N4*0' 'M2N5*0' 'M3N4*0' 'M3N5*0' 'M4N5*0' 'N1M2*1' 'N1M3*0' 'N1M4*0' 'N1M5*0' 'N2M3*0' 'N2M4*0' 'N2M5*0' 'N3M4*0' 'N3M5*0' 'N4M5*0'] ['M1N2*1' 'M1N3*0' 'M1N4*0' 'M1N5*0' 'M2N3*0' 'M2N4*0' 'M2N5*0' 'M3N4*0' 'M3N5*0' 'M4N5*0' 'N1M2*0' 'N1M3*0' 'N1M4*0' 'N1M5*0' 'N2M3*0' 'N2M4*0' 'N2M5*0' 'N3M4*0' 'N3M5*0' 'N4M5*0'] ['M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 'N1M5*0' 'N2M3*0' 'N2M4*0' 'N2M5*0' 'N3M4*0' 'N3M5*0' 'N4M5*0'] ['M1N2*1' 'M1N3*0' 'M1N4*0' 'M1N5*0' 'M2N3*0' 'M2N4*0' 'M2N5*0' 'M3N4*0' 'M3N5*0' 'M4N5*0' 'N1M2*0' 'N1M3*0' 'N1M4*0' 'N1M5*0' 'N2M3*0' 'N2M4*0' 'N2M5*0' 'N3M4*0' 'N3M5*0' 'N4M5*0'] ['M1N2*0' 'M1N3*0' 'M1N4*0' 'M1N5*0' 'M2N3*0' 'M2N4*0' 'M2N5*0' 'M3N4*0' 'M3N5*0' 'M4N5*0' 'N1M2*0' 'N1M3*1' 'N1M4*0' 'N1M5*0' 'N2M3*0' 'N2M4*0' 'N2M5*0' 'N3M4*0' 'N3M5*0' 'N4M5*0'] ['M1N2*0' 'M1N3*1' 'M1N4*0' 'M1N5*0' 'M2N3*0' 'M2N4*0' 'M2N5*0' 'M3N4*0' 'M3N5*0' 'M4N5*0' 'N1M2*0' 'N1M3*0' 'N1M4*0' 'N1M5*0' 'N2M3*0' 'N2M4*0' 'N2M5*0' 'N3M4*0' 'N3M5*0' 'N4M5*0'] ['M1N2*0' 'M1N3*0' 'M1N4*0' 'M1N5*0' 'M2N3*0' 'M2N4*0' 'M2N5*0' 'M3N4*0' 'M3N5*0' 'M4N5*0' 'N1M2*1' 'N1M3*0' 'N1M4*0' 'N1M5*0' 'N2M3*0' 'N2M4*0' 'N2M5*0' 'N3M4*0' 'N3M5*0' 'N4M5*0'] ['M1N2*1' 'M1N3*0' 'M1N4*0' 'M1N5*0' 'M2N3*0' 'M2N4*0' 'M2N5*0' 'M3N4*0' 'M3N5*0' 'M4N5*0' 'N1M2*0' 'N1M3*0' 'N1M4*0' 'N1M5*0' 'N2M3*0' 'N2M4*0' 'N2M5*0' 'N3M4*0' 'N3M5*0' 'N4M5*0']]



RE: First for loop stops after first iteration - buran - Apr-30-2019

because you have break on line 17


RE: First for loop stops after first iteration - Divanova94 - Apr-30-2019

(Apr-30-2019, 12:42 PM)buran Wrote: break
without the break statement the output is the following one
Output:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-170-4c6aef3bebbf> in <module> 16 return new1.reshape( shape ) 17 ---> 18 print (get_matrix(f1, f2)) <ipython-input-170-4c6aef3bebbf> in get_matrix(f1, f2) 13 new1 = np.array(new) 14 shape = ( 30, 20 ) ---> 15 new1.reshape( shape ) 16 return new1.reshape( shape ) 17 ValueError: cannot reshape array of size 3200 into shape (30,20)



RE: First for loop stops after first iteration - buran - Apr-30-2019

what shape do you want? 30x20 does not work


RE: First for loop stops after first iteration - Divanova94 - Apr-30-2019

(Apr-30-2019, 01:41 PM)buran Wrote: what shape do you want? 30x20 does not work

Yes it should be 30x20.
But I can simplify the example. The point is to create new lists that for the elements in f1 will *1 and for the missing elements comparing to f2 will add *0.

f1 = [['N1M2', 'N1M3', 'N1M4', 'N1M5'], ['M1N2', 'N2M3', 'N2M4', 'N2M5'], ['N1M3', 'N1M4', 'N1M5', 'N2M3', 'N2M4', 'N2M5']]

f2 = ['M1N2','M1N3','M1N4','M1N5','M2N3','M2N4','M2N5','M3N4','M3N5','M4N5','N1M2','N1M3','N1M4','N1M5','N2M3','N2M4','N2M5','N3M4','N3M5','N4M5']
I don't know how I can loop correctly to solve this problem.


RE: First for loop stops after first iteration - buran - Apr-30-2019

30X20 would be 600 elements and you have 3200 (160x20). When you are reshaping, the total number of elements can’t be altered. That is why it works with break - f1 has 30 sub-lists and with brake it takes first element of each so you get 30x20 array to reshape
with the simplified example would you share what desired output would be and how it is achieved


RE: First for loop stops after first iteration - Divanova94 - May-01-2019

(Apr-30-2019, 04:34 PM)buran Wrote: 30X20 would be 600 elements and you have 3200 (160x20). When you are reshaping, the total number of elements can’t be altered. That is why it works with break - f1 has 30 sub-lists and with brake it takes first element of each so you get 30x20 array to reshape
with the simplified example would you share what desired output would be and how it is achieved

f1 = [['N1M2', 'N1M3', 'N1M4', 'N1M5']]

f2 = ['M1N2','M1N3','M1N4','M1N5','M2N3','M2N4','M2N5','M3N4','M3N5','M4N5','N1M2','N1M3','N1M4','N1M5','N2M3','N2M4','N2M5','N3M4','N3M5','N4M5']
new = []
def get(f1, f2):
    for j in f1:
        for x in j:
            for i in f2:
                if i == x:
                    new.append('%s*1' % i)
                else:
                    new.append('%s*0' % i)
    return new
Output:
['M1N2*0', 'M1N3*0', 'M1N4*0', 'M1N5*0', 'M2N3*0', 'M2N4*0', 'M2N5*0', 'M3N4*0', 'M3N5*0', 'M4N5*0', 'N1M2*1', 'N1M3*0', 'N1M4*0', 'N1M5*0', 'N2M3*0', 'N2M4*0', 'N2M5*0', 'N3M4*0', 'N3M5*0', 'N4M5*0', 'M1N2*0', 'M1N3*0', 'M1N4*0', 'M1N5*0', 'M2N3*0', 'M2N4*0', 'M2N5*0', 'M3N4*0', 'M3N5*0', 'M4N5*0', 'N1M2*0', 'N1M3*1', 'N1M4*0', 'N1M5*0', 'N2M3*0', 'N2M4*0', 'N2M5*0', 'N3M4*0', 'N3M5*0', 'N4M5*0', 'M1N2*0', 'M1N3*0', 'M1N4*0', 'M1N5*0', 'M2N3*0', 'M2N4*0', 'M2N5*0', 'M3N4*0', 'M3N5*0', 'M4N5*0', 'N1M2*0', 'N1M3*0', 'N1M4*1', 'N1M5*0', 'N2M3*0', 'N2M4*0', 'N2M5*0', 'N3M4*0', 'N3M5*0', 'N4M5*0', 'M1N2*0', 'M1N3*0', 'M1N4*0', 'M1N5*0', 'M2N3*0', 'M2N4*0', 'M2N5*0', 'M3N4*0', 'M3N5*0', 'M4N5*0', 'N1M2*0', 'N1M3*0', 'N1M4*0', 'N1M5*1', 'N2M3*0', 'N2M4*0', 'N2M5*0', 'N3M4*0', 'N3M5*0', 'N4M5*0']
but the desired output is:
Output:
['M1N2*0', 'M1N3*0', 'M1N4*0', 'M1N5*0', 'M2N3*0', 'M2N4*0', 'M2N5*0', 'M3N4*0', 'M3N5*0', 'M4N5*0', 'N1M2*1', 'N1M3*1', 'N1M4*1', 'N1M5*1', 'N2M3*0', 'N2M4*0', 'N2M5*0', 'N3M4*0', 'N3M5*0', 'N4M5*0']
How could I get this?


RE: First for loop stops after first iteration - buran - May-01-2019

still not sure I understand, but this will produce the desired result. requires python 3.6+

f1 = [['N1M2', 'N1M3', 'N1M4', 'N1M5']]
 
f2 = ['M1N2','M1N3','M1N4','M1N5','M2N3','M2N4','M2N5','M3N4','M3N5','M4N5','N1M2','N1M3','N1M4','N1M5','N2M3','N2M4','N2M5','N3M4','N3M5','N4M5']

new = []
for item in f2:
    for sub_list in f1:
        new_item = f'{item}*{int(item in sub_list)}'
        new.append(new_item)
print(new)



RE: First for loop stops after first iteration - Divanova94 - May-01-2019

(May-01-2019, 02:10 PM)buran Wrote: still not sure I understand, but this will produce the desired result. requires python 3.6+
f1 = [['N1M2', 'N1M3', 'N1M4', 'N1M5']] f2 = ['M1N2','M1N3','M1N4','M1N5','M2N3','M2N4','M2N5','M3N4','M3N5','M4N5','N1M2','N1M3','N1M4','N1M5','N2M3','N2M4','N2M5','N3M4','N3M5','N4M5'] new = [] for item in f2: for sub_list in f1: new_item = f'{item}*{int(item in sub_list)}' new.append(new_item) print(new)

Thank you very much, but in case with two lists in f1
f1 = [['N1M2', 'N1M3', 'N1M4', 'N1M5'], ['M1N2', 'N2M3', 'N2M4', 'N2M5']]
f2 = ['M1N2','M1N3','M1N4','M1N5','M2N3','M2N4','M2N5','M3N4','M3N5','M4N5','N1M2','N1M3','N1M4','N1M5','N2M3','N2M4','N2M5','N3M4','N3M5','N4M5']
 
new = []
for item in f2:
    for sub_list in f1:
        new_item = f'{item}*{int(item in sub_list)}'
        new.append(new_item)
print(new)
It will give the output:
Output:
['M1N2*0', 'M1N2*1', 'M1N3*0', 'M1N3*0', 'M1N4*0', 'M1N4*0', 'M1N5*0', 'M1N5*0', 'M2N3*0', 'M2N3*0', 'M2N4*0', 'M2N4*0', 'M2N5*0', 'M2N5*0', 'M3N4*0', 'M3N4*0', 'M3N5*0', 'M3N5*0', 'M4N5*0', 'M4N5*0', 'N1M2*1', 'N1M2*0', 'N1M3*1', 'N1M3*0', 'N1M4*1', 'N1M4*0', 'N1M5*1', 'N1M5*0', 'N2M3*0', 'N2M3*1', 'N2M4*0', 'N2M4*1', 'N2M5*0', 'N2M5*1', 'N3M4*0', 'N3M4*0', 'N3M5*0', 'N3M5*0', 'N4M5*0', 'N4M5*0']
But I need this output:
Output:
['M1N2*0', 'M1N3*0', 'M1N4*0', 'M1N5*0', 'M2N3*0', 'M2N4*0', 'M2N5*0', 'M3N4*0', 'M3N5*0', 'M4N5*0', 'N1M2*1', 'N1M3*1', 'N1M4*1', 'N1M5*1', 'N2M3*0', 'N2M4*0', 'N2M5*0', 'N3M4*0', 'N3M5*0', 'N4M5*0'] ['M1N2*1', 'M1N3*0', 'M1N4*0', 'M1N5*0', 'M2N3*0', 'M2N4*0', 'M2N5*0', 'M3N4*0', 'M3N5*0', 'M4N5*0', 'N1M2*0', 'N1M3*0', 'N1M4*0', 'N1M5*0', 'N2M3*1', 'N2M4*1', 'N2M5*1', 'N3M4*0', 'N3M5*0', 'N4M5*0']



RE: First for loop stops after first iteration - Divanova94 - May-01-2019

(May-01-2019, 03:01 PM)Divanova94 Wrote: [quote="buran" pid="79039" dateline="1556719854"]still not sure I understand, but this will produce the desired result. requires python 3.6+
f1 = [['N1M2', 'N1M3', 'N1M4', 'N1M5']] f2 = ['M1N2','M1N3','M1N4','M1N5','M2N3','M2N4','M2N5','M3N4','M3N5','M4N5','N1M2','N1M3','N1M4','N1M5','N2M3','N2M4','N2M5','N3M4','N3M5','N4M5'] new = [] for item in f2: for sub_list in f1: new_item = f'{item}*{int(item in sub_list)}' new.append(new_item) print(new)
Thank you very much, It worked :)