Tuesday, 15 April 2014

Efficient Python search algorithm to find matches over moving time intervals -


I have a list of dictionaries like:

  listofdicts = [{' Time ': 2015-03-14 11:54:00,' value ':' some value '}, {' time ': 2015-03-14 13:23:00,' value ':' other values'}, {'Time': 2015-03-14 12:52:00, 'value': 'some values'}, ...]  

I search for dictionaries with the following criteria Want to list: Look at three or more dictionaries that have the same value for the price, with time value within 10 minutes of each other. I would like to make this algorithm a new key that matches each criterion that matches this criteria and mark it as a match.

  will find search algorithms: {'time': 2015-03-14 11:54:00, 'value': 'same value'} {'time': 2015-03-14 11 : 56: 00, 'Value': 'Equal Value'} {'Time': 2015-03-14 11:52:00, 'Value': 'Equal Value'} and Add Match Key to Each Dictionary: {'Time ':' Value ':' Equal Value ',' Match ':' Truth '} {' Time ': 2015-03-14 11:56:00,' Value ': 'Equal value', 'match': 'true'} {'time': 2015-03-14 11:52:00, 'value': 'equal value', 'match': 'true'}  

I have already made an algorithm to do this, but this special rupee Is not efficient or scalable.

Currently in the listofdicts for algorithm:

 : matchdicts = [] in listofdicts dict2: if dict ['value'] == dict2 ['value ']: Matchdicts.append (dict2) listoftimeintervals = [[dict [' time '] - datetime.timedelta (minutes = 10), dict [' time ']], [word [' time '] - datetime.timedelta (min = 9), dict ['time'] + datetime.timedelta (minutes = 1)], ..., [dict "time" time "time"] + datetime.timedelta (min = 10)]] time For listoftimeintervals: dictsintimerange = [] Matching matches in matchesdicts: if time [0] & lt; = Matchingdict ['time'] & lt; = Time [1]: dictsintimerange.append (mailing dint) if for each diction in the lane (disquiminnerange)> = 3: decincenterange: each match ['matching'] == 'true'  
< / Div>

(Note: I also do not have to run this code by an interpreter.)

< P> The first division by value.

  import collection listofdictsbyvalue = collections.defaultdict (list) for d listofdicts: listofdictsbyvalue [d ['value']] .andand (d)  

Then sort each list by time and scan it.

  import operator k = 3 list (len (lst) - (k - 1)) in: listofdictsbyvalue.values ​​(i) (list of list in i + (k - 1) ): Lst.sort (key = operator.itemgetter ('time')) ['time'] - lst [i] ['time']  

No comments:

Post a Comment