利用python把数组中的每一个元素拓展为一个数组

2025-02-24 17:30:10
推荐回答(1个)
回答1:

if __name__ =='__main__':
   y = [0, 2, 1, 2, 0, 1, 0, 0, 2, 1]
   result =[]
   for i in range(len(y)):
      if y[i] == 0:
         result.extend([1,0,0])
      elif y[i] == 1:
         result.extend([0,1,0])
      elif y[i] == 2:
         result.extend([0,0,1])
      else:
         result.append(y[i])
   print(result)

这里新开了一个result,增加了内存开销