python - Why can't I iterate through list of lists this way? -
sorry i'm new python, needed take 6 individual lists , concatenate them such resemble list of lists.
i.e. a1 list + b1 list b + c1 list c , a2 list + b2.... etc
should become [[a1,b1,c1], [a2,b2,c2]...]
i tried this:
comblist = [[0]*6]*len(lengthlist) in range(len(lengthlist)): print comblist[i][0] = poslist[i] comblist[i][1] = widthlist[i] comblist[i][2] = heightlist[i] comblist[i][3] = arealist[i] comblist[i][4] = perimlist[i] comblist[i][5] = lengthlist[i] # i++ print comblist
and tried variation appended instead:
in range(len(lengthlist)): print comblist[i][0].append(poslist[i]) comblist[i][1].append(widthlist[i]) comblist[i][2].append(heightlist[i]) comblist[i][3].append(arealist[i]) comblist[i][4].append(perimlist[i]) comblist[i][5].append(lengthlist[i]) # i++ print comblist
so have 2 questions.
why didn't either of work, cus in mind should have. , don't need put i++ @ bottom right? reason wasn't working trouble shooting.
i ended finding solution, below, i'd understand happened in above 2 codes failed terribly.
for j in range(len(fnamelist)): rows = [fnamelist[j], widthlist[j], heightlist[j], arealist[j], perimeterlist[j], lengthlist[j]] print rows comblist.append(rows) print comblist
the issue @ did creating list of 6 references same thing.
[0]*6
generate list of 6 references same number (zero), , [[0]*6]*len(lengthlist)
generate list of references same [0]*6
list.
i think function want zip
:
a = ['a1','a2','a3'] b = ['b1','b2','b3'] c = ['c1','c2','c3'] print [x x in zip(a,b,c)]
which gives:
[('a1', 'b1', 'c1'), ('a2', 'b2', 'c2'), ('a3', 'b3', 'c3')]
so in case, work:
comblist = [x x in zip(fnamelist, widthlist, heightlist, arealist, perimeterlist, lengthlist)]
Comments
Post a Comment