Improving efficiency in iterating over list sets in python -


i have list of lists has items in element 0 , value associated in element 1. each item appear more once. create list of uniques items max value associated each one. code accomplishes this, seems inefficient. also, simplified example. mylist 100,000 rows. suggestions of improving efficiency?

mylist = [['item 1', 12],['item 1', 10], ['item 3', 12],['item 4', 10], ['item 3', 14]]  # unique items my_unique_items = list(set(x[0] x in mylist))  # make list of list my_unique_items = [[x] x in my_unique_items]  # iterate on list items item in my_unique_items:      # list comp max value , append     item.append(max([x[1] x in mylist if x[0] == item[0]]))  print my_unique_items 

it more efficient loop through mylist once. if care max value each item key, keep mapping of items , max values , compare them go through list.

this has worst case of o(n), whereas original had worst case of o(n^2).

item_maxes = {} item in mylist:     max_value = item_maxes.setdefault(item[0], none)     if max_value none or item[1] > max_value:         item_maxes[item[0]] = item[1] 

edit: think shadowranger's version of method cleaner looking:

max_vals = {} item, value in mylist:     max_vals[item] = max(max_vals.get(item, value), value) 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -