在该程序中给出了用户输入列表。我们的任务是复制或克隆列表。这里我们使用切片技术。在这种技术中,我们将复制列表本身以及参考。此过程也称为克隆。
Step 1: Input elements of the array. Step 2: then do cloning using slicing operator(:).
# Python program to copy or clone a list # Using the Slice Operator def copyandcloning(cl): copylist = cl[:] return copylist # Driver Code A=list() n1=int(input("Enter the size of the List ::")) print("Enter the Element of List ::") for i in range(int(n1)): k=int(input("")) A.append(k) clon = copyandcloning(A) print("Original or Before Cloning The List Is:", A) print("After Cloning:", clon)
输出结果
Enter the size of the List ::6 Enter the Element of List :: 33 22 11 67 56 90 Original or Before Cloning The List Is: [33, 22, 11, 67, 56, 90] After Cloning: [33, 22, 11, 67, 56, 90]