If I want to get the value when the last loop completed,I can use last_a=a
But how to get the value when the first 2 loops are completed
Input:
A=[20,13,15,25,24,17,14,19,23,11]
B=[32,43,65,21,13,16,53,52,35,38]
last_a=last_b=0
last_last_a=0
last_last_b=0
for a,b in zip(A, B):
if a<last_a and b<last_b:
pass
last_a=a
last_b=b
print(last_last_a)
Expected Output
0
0
20
13
15
25
24
17
14
19
CodePudding user response:
You need to add last_last_a = last_a
before assigning to last_a
. Also, you need to print before making those assignments, as these assignments serve for the next iteration of the loop. So:
for a,b in zip(A, B):
if a<last_a and b<last_b:
pass
print(last_last_a) # moved up
last_last_a = last_a # <-- added
last_a=a
last_b=b
As you don't print last_last_b
, there is no need for that name.
CodePudding user response:
You can use enumerate
with zip
to keep track.
And then just use required index.
for index, (name, age) in enumerate(zip(names, ages)):
...
CodePudding user response:
If you want to output the value after two loops, just track the loops with a variable like i
. However, if you want a value
after the first 2 loops are completed
you will not get a list as written in expected Output but one value (or more if you add print statements). I think your logic is off, since last_last_a
is never modified.
A=[20,13,15,25,24,17,14,19,23,11]
B=[32,43,65,21,13,16,53,52,35,38]
last_a=last_b=0
last_last_a=0
last_last_b=0
for i, (a,b) in enumerate(zip(A, B)):
if a<last_a and b<last_b:
pass
last_a=a
last_b=b
if i==2:
print(last_last_a)