אז באחת הדוגמאות של הפעולה id
יש את התרגול הבא:
גרמו לכך שהקוד שמוסיף את הערך 100 לרשימה הראשונה לא ישפיע על שאר הרשימות.
collections_of_numbers = [[0, 0, 0]] * 3
print(collections_of_numbers)
collections_of_numbers[0].append(100)
print(collections_of_numbers)
אז כדי להוסיף 100 רק לתת רשימה הראשונה, שיניתי את השורה הראשונה ל:
collections_of_numbers = [[0, 0, 0], [0, 0, 0], [0, 0, 0]
וזה עבד.
אבל, למה בשימוש ב
copy
זה לא מסתדר?
למשל:
collections_of_numbers = [[0, 0, 0]]
x = collections_of_numbers.copy()
new_collections_of_numbers = collections_of_numbers + x + x
print(new_collections_of_numbers)
new_collections_of_numbers[0].append(100)
print(new_collections_of_numbers)