-1

Currently I have a new list within my loop that for the most part takes creates a new list with almost all the elements of the original list x in data. However, I'm making a conditional that increments a specific index of xnew only if a condition is x[5] is met. Is there a cleaner way to do the following?:

for x in data:
  xnew = [x[1],x[2],x[3],x[4],0,0,0]
  if(x[5]==2):
    xnew[4] = x[8] 
  elif(x[5]==9):
    xnew[5] = x[8] 
  else:
    xnew[6] = x[8]
4

1 に答える 1

8
xmap = {2: 4, 9: 5}

 ...

xnew[xmap.get(x[5], 6)] = x[8]
于 2012-09-07T14:54:44.870 に答える