I have a list of numbers and I need to split into to corresponding arrays of different sizes, but that make up all the combinations of the array splitting up. For example, if I have an array a=[1,2,3,4,5] and I want to split it to one array of size 3 and the other 2.
So I was thinking of making two arrays to hold each array and since there’s the same number of size 3 and size 2 arrays I could match them up and then perform my tests. (it’s a stats class so if there is a better scipy or numpy implementation I’d love to hear it as I’d like to move use those, in the end I’d like to get the all the differences of means between the different arrays)
But for my code here it is
import itertools
#defines the array of numbers and the two columns
number = [53, 64, 68, 71, 77, 82, 85]
col_one = []
col_two = []
#creates an array that holds the first four
results = itertools.combinations(number,4)
for x in results:
col_one.append(list(x))
print col_one
#attempts to go through and remove those numbers in the first array
#and then add that array to col_two
for i in range(len(col_one)):
holder = number
for j in range(4):
holder.remove(col_one[i][j])
col_two.append(holder)
Thanks in advance
EDIT: it seems the spacing of the code it messed up – I assure you the spacing is ok although when I run the code I can’t remove an item from the holder since it’s not there.
I tested your code and I see the problem. In this code,
the line
holder = numberdoesn’t copynumber, it just givesnumbera second name,holder. Then, when you remove things fromholder, they’re also removed fromnumber, so when the loop goes around again, number has four fewer numbers in it. Ad infinitum.You want to make a copy of number:
This creates a new list from
numbercalledholder. Now onlyholderis changed.would also work.
You should also use
for‘s full potential by avoiding index variables:This does the same thing, is easier to read and probably faster to boot.
Now for the next step, list comprehensions. This is a great way to avoid nested loops.
This does the same thing as above. You can even make this a one-liner:
Combining it all together: