Python Forum

Full Version: Numpy: repeat cells variable number of times
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

In the following snippet, each cell of CellsToDuplicateVariableTimes is repeated the number of times defined in NumberOfReplicationPerCell : i'm sure there's a more relevant way to proceed => how would you do?

Thanks

Paul

CellsToDuplicateVariableTimes = np.array([2, 6, 8, 10, 11])
NumberOfReplicationPerCell = np.array([4, 3, 2, 5, 1])

New = np.zeros(0, dtype = int)
n = np.shape(CellsToDuplicateVariableTimes)[0]
List = [np.repeat(CellsToDuplicateVariableTimes[i], NumberOfReplicationPerCell[i]) for i in range(n)]
for arr in List: New = np.hstack((New, arr))

print(f"New = {New}")
Output:
New = [ 2 2 2 2 6 6 6 8 8 10 10 10 10 10 11]