Warum ändert .append () in meiner Liste den Wert jeder Mitgliedsvariablen in die neue Variable?
In meiner Funktion erstelle ich eindeutige Variablen, die ich einer Liste hinzufügen möchte. Aber wenn ich die nächste Variable anhänge, ändern sich die Werte aller anderen Variablen in der Liste in die neue.
Hier ist mein Code:
def make_list_of_data_transfer_objects(iFile, eFile, index_of_sheet):
iBook = open_workbook(iFile)
iSheet = iBook.sheet_by_index(0)
eBook = open_workbook(eFile)
eSheet = eBook.sheet_by_index(index_of_sheet)
DataSet = namedtuple('DataSet', 'line_num data_list')
list_objects = []
temp_line_num = 99999
temp_data = [0]*5
for row_index in range(eSheet.nrows):
temp_data[0] = eSheet.cell(row_index,0).value
temp_data[1] = eSheet.cell(row_index,1).value
temp_data[2] = eSheet.cell(row_index,2).value
temp_data[3] = eSheet.cell(row_index,3).value
temp_data[4] = eSheet.cell(row_index,4).value
for row_index2 in range(iSheet.nrows):
if temp_data[0] == iSheet.cell(row_index2,0).value:
temp_line_num = row_index2
temp_object = DataSet(temp_line_num, temp_data)
list_objects.append(temp_object)
#print list_objects #every object is the same
list_objects.sort(key = lambda tup: tup[0]) #sort by line number
return list_objects