Python Forum
First for loop stops after first iteration
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
First for loop stops after first iteration
#1
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'] ['M1N2*0' 'M1N3*0' 'M1N4*0' 'M1N5*0' 'M2N3*0' 'M2N4*0' 'M2N5*0' 'M3N4*0' 'M3N5*0' 'M4N5*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1N4*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']]
Reply
#2
because you have break on line 17
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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)
Reply
#4
what shape do you want? 30x20 does not work
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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.
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(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?
Reply
#8
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(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']
Reply
#10
(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 :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  saving each iteration of a loop sgcgrif3 3 213 Jul-27-2021, 01:02 PM
Last Post: DeaD_EyE
  IDLE stops responding upon saving tompi1 2 538 Oct-01-2020, 05:44 PM
Last Post: Larz60+
  String slicing and loop iteration divyansh 9 1,745 Jun-07-2020, 10:29 PM
Last Post: divyansh
  Python timer script stops before should ozstar 3 871 May-04-2020, 12:55 AM
Last Post: ozstar
  Python stops without errors shahgourav 4 1,058 Feb-04-2020, 11:44 PM
Last Post: micseydel
  Changing a variable's name on each iteration of a loop rix 6 24,359 Jan-03-2020, 07:06 AM
Last Post: perfringo
  Parallel iteration with for loop Josh_Python890 1 1,045 Jul-19-2019, 11:50 PM
Last Post: metulburr
  Multiprocessing my Loop/Iteration (Try...Except) Jompie96 7 1,935 Jun-19-2019, 12:59 PM
Last Post: noisefloor
  file.write stops while true loop from executing in python3 boonr 3 1,287 Mar-25-2019, 12:50 PM
Last Post: ichabod801
  Python Thread stops execution neethuvp 1 1,312 Feb-18-2019, 06:36 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020