ในโปรแกรมนี้ได้รับรายการอินพุตของผู้ใช้ งานของเราคือคัดลอกหรือโคลนรายการ ที่นี่เราใช้เทคนิคการหั่น ในเทคนิคนี้ เราสร้างสำเนาของรายการพร้อมกับข้อมูลอ้างอิง กระบวนการนี้เรียกอีกอย่างว่าการโคลน
อัลกอริทึม
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]