1

let's say I have a master array of 365 dates in YYYYMMDD format. I want to create, for the sake of this example, 3 different lists that are unique, but also use up the entirety of the 365 dates.

For an overall example, let's take the year of 2017. I would like to create 3 lists from the 365 days that are mutually exclusive and collectively exhaustive. In other words, none of the dates in array1 will be in array2 nor array3, and the dates in array2 will not be in array3. Array1 will have a length of 300, array2 will have a length of 64, and array3 will simply be a length of 1.

I know this can be achieved with lists and sets, and potentially by including 'not in', but I have been going in circles with nothing working. What would be the best solution for a problem like this?

martineau
  • 119,623
  • 25
  • 170
  • 301
WX_M
  • 458
  • 4
  • 20

2 Answers2

3

Perhaps use shuffle to randomize the array, then split up the resulting shuffled list?

shuffle(arr)
arr1 = arr[0:300]
arr2 = arr[300:364]
arr3 = arr[364:365]
IronMan
  • 1,854
  • 10
  • 7
0

I would create the dates easily with pandas, transform into a numpy array, and then use numpy.random.shuffle:

import pandas as pd
import numpy as np
list_of_dates = pd.date_range('2019-01-01', periods=365, freq='D') #Creates the list with all the dates for the year 2019.
dates = np.array(list_of_dates) #Transform it into a np.array to apply random.shuffle() later
np.random.shuffle(dates) #randomize the order of the list
list_1 = dates[0:300] #create 1st list
list_2 = dates[301:364] #create 2nd list
list_3 = dates[364:-1] #create 3rd list

Test:

print(list_1[0:3])

Output:

['2019-01-19T00:00:00.000000000' '2019-04-04T00:00:00.000000000'
 '2019-03-10T00:00:00.000000000']
Celius Stingher
  • 17,835
  • 6
  • 23
  • 53