What's the difference between li [:] and li here?

topic description

about the full permutation of arrays

sources of topics and their own ideas

python%E6%A0%87%E5%87%86%E7%AE%97%E6%B3%95%E5%AE%9E%E7%8E%B0%E6%95%B0%E7%BB%84%E5%85%A8%E6%8E%92%E5%88%97%E7%9A%84%E6%96%B9%E6%B3%95/" rel=" nofollow noreferrer "> http://www.codeweblog.com/pyt.

related codes

/ / Please paste the code text below (do not replace the code with pictures)

def Mideng(li):
    if(type(li) != list):
        return
    if(len(li) == 1):
        return [li]
    result = []
    for i in range(len(li[:])):
        bak = li[:]
        head = bak.pop(i)
        for j in Mideng(bak):
            j.insert(0,head)
            result.append(j)
    return result
def MM(n):
    if (type(n) != int  or n < 1):
        return
    return Mideng(list(range(1, n+1)))

print(MM(6))

what result do you expect? What is the error message actually seen?

if li is written in the for loop instead of li [:], it will report an error
RecursionError: maximum recursion depth exceeded in comparison
what is the difference between li and li [:] here?

Jun.24,2021

li [:] makes a copy of li.
if you use li, directly, it is equivalent to pointing bak to the same address of li, while bak.pop operation bak, is also operating li. This will lead to the formation of an endless cycle.

Menu