I have a piece of code that I would like to re-write in order for it to be more effective. I am working in Python, and there is not switch-break statement like in Java, but I feel something of a similar structure would work. The piece of code in particular I am talking about is:
def update(arm, array1, array2, array3, array4, array5, array6, array7, array8, array9, array10):
# If the array is empty, add a zero
if not array1:
array1.append(0)
if not array2:
array2.append(0)
if not array3:
array3.append(0)
if not array4:
array4.append(0)
if not array5:
array5.append(0)
if not array6:
array6.append(0)
if not array7:
array7.append(0)
if not array8:
array8.append(0)
if not array9:
array9.append(0)
if not array10:
array10.append(0)
# If the chosen arm doesn't match, then add the last value that was added before to the end
if arm != 0:
array1.append(array1[-1])
if arm != 1:
array2.append(array2[-1])
if arm != 2:
array3.append(array3[-1])
if arm != 3:
array4.append(array4[-1])
if arm != 4:
array5.append(array5[-1])
if arm != 5:
array6.append(array6[-1])
if arm != 6:
array7.append(array7[-1])
if arm != 7:
array8.append(array8[-1])
if arm != 8:
array9.append(array9[-1])
if arm != 9:
array10.append(array10[-1])
In general, what is a good way to avoid writing a series of if-statements like this in Python while also going through and checking each of these arrays for this particular function?
Aucun commentaire:
Enregistrer un commentaire