Python List Copy Question (copy module, slice)

Asked 2 years ago, Updated 2 years ago, 18 views

import copy

a = ["Aa","Bb", "Cc"]
b = ["11", "22", "33"]
c = []

a.extend(b)
c.append(a)
c.append(a)

print("1",c) #Results(1 ['Aa', 'Bb', 'Cc', '11', '22', '33'], ['Aa', 'Bb', 'Cc', '11', '22', '33'])

c = c[:]

del a[3:]

print("2", c) #Result(2 ["Aa", "Bb", "Cc", ["Aa", "Bb", "Cc"])

I want to print out "Aa", "Bb", "Cc", "11", "22", "33", [Aa", "Bb", "Cc", "11", "22", "33"] after [3:] The elements of a[] are still referenced and reflected in the following values:.

If c = copy.deepcopy(c) is repeatedly used, the error message maximum recovery depth exceeded in comparison appears.

Even if you add the code below and run it, there seems to be a difference in processing speed I'd like to copy in c = c[ : ]. Is there a way?

import sys
 __name__=='__main__':
    sys.setrecursionlimit(2000)

Source: https://tmdahr1245.tistory.com/97 [tmdahr1245]

python

2022-09-22 19:25

1 Answers

c.append(a[:])


2022-09-22 19:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.