I have a large nested loop called classarray full of information about different classes. I'm trying to combine the classes that are the same class, given that the data in the first 2 nest loop indexes are the same.
[['AAS', '100', 'Intro Asian American Studies', '0', '12', '8', '5', '1', '3', '0', '0', '0', '0', '0', '0', '0', 'S-15']
['AAS', '100', 'Intro Asian American Studies', '1', '10', '4', '3', '6', '0', '1', '2', '0', '0', '0', '0', '0', 'S-15']
['AAS', '100', 'Intro Asian American Studies', '1', '7', '6', '7', '4', '1', '0', '1', '0', '0', '0', '0', '0', 'S-15']
['AAS', '120', 'Intro to Asian Am Pop Culture', '6', '7', '5', '2', '0', '3', '3', '0', '1', '0', '0', '0', '1', 'S-15']
['AAS', '215', 'US Citizenship Comparatively', '1', '3', '5', '4', '1', '6', '1', '3', '2', '1', '1', '1', '0', 'F-15']
['AAS', '258', 'Muslims in America', '0', '19', '2', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', 'S-16']
['AAS', '365', 'Asian American Media and Film', '1', '4', '6', '4', '5', '1', '3', '2', '2', '0', '0', '1', '0', 'S-16']
['AAS', '365', 'Asian American Media and Film', '6', '15', '0', '0', '1', '0', '3', '1', '1', '0', '0', '0', '2', 'S-15']]
I've written this code below to try to combine the rows and produce a new array called new_array:
itemx = ['','','',0,0,0,0,0,0,0,0,0,0,0,0,0,'']
newarray=[]
for course,courseplus1 in zip(classarray,classarray[1:]):
if (course[0]==courseplus1[0] and course[1]==courseplus1[1]):
itemx[0]=course[0]
itemx[1]=course[1]
itemx[2]=course[2]
for number in range(3,16):
itemx[number]=itemx[number]+(int(course[number])+int(courseplus1[number]))
itemx[16]=course[16]
else:
newarray.append(itemx)
print(itemx)
itemx[0]=courseplus1[0]
itemx[1]=courseplus1[1]
itemx[2]=courseplus1[2]
for number in range(3,16):
itemx[number]=int(courseplus1[number])
for item in newarray:
print(item)
However, the output is this:
['AAS', '365', 'Asian American Media and Film', '7', '19', '6', '4', '6', '1', '6', '3', '3', '0', '0', '1', '2', 'S-16']
5 times. From what I understood by looking through stack overflow, the reason is because:
newarray.append(itemx)
appends itemx to the list; itemx is one singular memory location that, at the end, has AAS 365's information in it. So, new array, as a list of itemx's, has a bunch of itemx's in it.
My question is: how do I deal with or mitigate this problem? To create classarray, I was doing the same thing, except i was declaring the itemx inside of the for loop, which i understand to mean itemx is a new variable with a new location.