recently I started to learn python, but I don"t quite understand about args
the code in the textbook is used to find out the intersection, and the process is somewhat incomprehensible
.def intersect(*a):
res = []
for x in a[0]:
print("up ",x)
for y in a[1:]:
print("down",y)
if x not in y :
break
else:
res.append(x)
print("res ",res)
return res
L = [1,2,4,6,8,16,32,64];L2 = [2,8,16,32,64,128];L3 = [4,6,8,16]
intersect(L,L2,L3)
I added the print of the website myself in order to understand the execution process
the end of the execution is like this:
up 1
down [2, 8, 16, 32, 64, 128]
up 2
down [2, 8, 16, 32, 64, 128]
down [4, 6, 8, 16]
up 4
down [2, 8, 16, 32, 64, 128]
up 6
down [2, 8, 16, 32, 64, 128]
up 8
down [2, 8, 16, 32, 64, 128]
down [4, 6, 8, 16]
res [8]
up 16
down [2, 8, 16, 32, 64, 128]
down [4, 6, 8, 16]
res [8, 16]
up 32
down [2, 8, 16, 32, 64, 128]
down [4, 6, 8, 16]
up 64
down [2, 8, 16, 32, 64, 128]
down [4, 6, 8, 16]
[8, 16]
I don"t understand why it is like the first cycle to check [2,8,16,32,64,128] and then go directly back to check 2. Shouldn"t we first finish the internal loop run and continue to check [4,68,16]? The same is true for the subsequent check. A [1] went straight back to a [0] to get a new number. How should I understand it?