I have data from travel diaries which has been read in from a csv file. I have it set up as a dictionary with a bunch of lists. E.g.:
print diary['ID'][1] gives 123456789
print diary['TravelReferenceDay'][1] gives 1 for a Monday
I want to randomnly select an ID from the array based on the day e.g.:
random.choice(diary['ID']) if diary['TravelReferenceDay'] == 1
I can arrange the data by TravelReferenceDay in the csv file. I had tried the groupby method to split up the array:
groups = []
uniquekeys = []
for k, g in groupby(diary, diary['TravelReferenceDay']):
groups.append(list(g)) # Store group iterator as a list
uniquekeys.append(k)
But that gave the error:
TypeError: 'list' object is not callable
Could you suggest a way to achieve this? Thanks.
My solution with a list comprehensions: