mardi 26 avril 2016

Creating 2 arrays from one array in python

I am new to python coming from a Java background and am trying to create two arrays from a single array where the odd indexs are put in one array and the even index are put into another.

array one will contain indexs(0,2,4,6,8 etc.) array two will contain indexs(1,3,5,7,9 etc.)

my current implementation is:

for i in range (0, len(originalArray) - 1):  
    if i % 2 == 0:
        Array1.append(originalArray[i])

    if i % 2 == 1:
        Array2.append(originalArray[i])

if len(originalArray) % 2 == 0:
    Array1.append(originalArray[-1])

Could anyone recommend a better way of doing this? Thanks.

Aucun commentaire:

Enregistrer un commentaire