Python beginners args problem, can you teach me?

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?

Mar.10,2021
The function of the

intersect function is to "find the intersection". The elements of intersection are the elements that appear in each set. If element a does not exist in set A1, then it is definitely not in the intersection, so there is no need to check the remaining sets A2, A3. So the first round uses 1 to check [2,8,16,32,64,128] , and finds that 1 not in a [1] then does not need to check a [2], then break , and then uses 2 to check.


to find the relationship of intersection is false, there is no need to continue to judge, the result is false.

Menu