0

I have a variable named records that looks like this:

records = {
    'set1': [{'Time Stamp': datetime, 'data': data, ...}, {'Time Stamp': datetime, 'data': data, ...}, ...],
    'set2': [{'Time Stamp': datetime, 'data': data, ...}, {'Time Stamp': datetime, 'data': data, ...}, ...]
    .
    .
    .
}

I'm trying to delete all the dictionaries (the ones with 'Time Stamp' in it) that don't have common time stamps amongst the other 'sets'.

My first thought was to create a variable from records that would look like this:

var = {
    'set1': [datetime, datetime, ...],
    'set2': [datetime, datetime, ...],
    .
    .
    .
}

And from there loop through something like list(set(a) & set(b)) (from here) to get a list of all the common time stamps.

How could I create the variable var? I assume it can be done with a list generator expression, I just don't know how to create them.

And if you don't think this is the right approach, how would you do it? In terms of time complexity, there won't ever be anymore than 10 'sets', but each set has around 4,000 dictionaries each with about 5 keys/values in them.

Edit: To simplify the question, how can you create a list from a list of dictionaries? To illustrate this:

dict = {'time': timevalue, 'otherstuff': othervalue}
list = [dict, dict, dict] # a list of dictionaries in the form of dict
# create result
result = [timevalue, timevalue, timevalue, timevalue]
Community
  • 1
  • 1
Ian
  • 218
  • 3
  • 11

2 Answers2

0
>>> import datetime
>>> 
>>> my_dict = {'time': datetime.datetime.now(), 'otherstuff': 'fooo'}
>>> my_list = [my_dict for i in xrange(3)]
>>> result = [d['time'] for d in my_list]
>>> result
[datetime.datetime(2014, 8, 19, 14, 13, 3, 620886), datetime.datetime(2014, 8, 19, 14, 13, 3, 620886), datetime.datetime(2014, 8, 19, 14, 13, 3, 620886)]
>>> 
Vor
  • 33,215
  • 43
  • 135
  • 193
-2

Something like this maybe:

var = {}
for set, list in records.iteritems():
    var[set] = [i['Time Stamp'] for i in list]
Mike
  • 6,813
  • 4
  • 29
  • 50