如何避免在 Python 中链接 3d 数组?

How do you avoid 3d arrays from being linked in Python?

我一直在尝试复制 3D 阵列,但似乎无论我做什么都无济于事;修改副本,也修改原件

我已经尝试了 this 问题中给出的最重要建议的多个版本。

任何人都可以向我解释为什么这段代码在复制后会修改 Clients 数组,以及如何避免这种情况?

##### This block is to get and save the information

import csv

import datetime

import glob
import os

#import copy

from datetime import timedelta

list_of_files = glob.glob(r'U:\UEL\Sales and Marketing\Relationship - Sales\Team\Edinburgh Clients\Portal backups\Full export archive\*.csv') # * means all if need specific format then *.csv
latest_file = max(list_of_files, key=os.path.getctime)

with open(latest_file, newline='') as csvfile:
    data = list(csv.reader(csvfile))
    Vheaders = data[0]
    data.pop(0)

Clients = list(set(list(zip(*data))[9]))

i = 0
for line in Clients:
    Clients[i] = [Clients[i]]
    i = i + 1

Voids = []
CoTs = []

for row in Clients:
    Voids.append(row)

for row in Clients:
    CoTs.append(row)

#Voids = list(Clients) #This creates an array of arrays, an array of voids for each client

#CoTs = list(Voids) #Same as above for CoTs

#import pdb
#pdb.set_trace()


i = 0

for row in Clients:
    for line in data:
        #if line[0] == '443179':
            #import pdb
            #pdb.set_trace()
        if (datetime.date.today() - timedelta(days=7)) <= datetime.datetime.strptime(line[2], "%d/%m/%Y").date() < datetime.date.today(): 
            if line[9] == row[0]:
                Voids[i].append(line)
            else:
                pass
        else:
            pass
    i = i + 1

您需要 deepcopy。 "Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other."