I try to create a function which allows me to group an array into groups.
arr = [1,2,3,4,5,6,7,8,9,10,11]
group1 = [1,1,1,1,1,1,2,2,2,2,2] # build 2 groups
group2 = [1,1,1,1,2,2,2,2,3,3,3] # build 3 groups
newarr = []
for index, item in enumerate(arr):
if index + 1 < len(arr) / 2:
newarr.append(1)
else:
newarr.append(2)
print(newarr)
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]
This works somewhat, but I am not able to scale that function in any way, since I would have to add if and else clauses if I want 3, 4, x groups. Is there a function to dynamically create my desired results?
I know in R is a seq function, but I was not able to find something similar in Python.