在我的函数中,我正在创建要添加到列表中的唯一变量。但是每当我附加下一个变量时,列表中所有其他变量的值都会更改为新变量。
这是我的代码:
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