def arrangeByType(service_list, preferred_types):
 def enumerate(elts):
  'Return an iterable that pairs the index of an element with\n        that element.\n\n        For Python 2.2 compatibility'
  return zip(range(len(elts)), elts)
 def bestMatchingService(service):
  'Return the index of the first matching type, or something\n        higher if no type matches.\n\n        This provides an ordering in which service elements that\n        contain a type that comes earlier in the preferred types list\n        come before service elements that come later. If a service\n        element has more than one type, the most preferred one wins.\n        '
  for (i, t) in enumerate(preferred_types):
   if (preferred_types[i] in service.type_uris):
    return i
  return len(preferred_types)
 prio_services = [(bestMatchingService(s), orig_index, s) for (orig_index, s) in enumerate(service_list)]
 prio_services.sort()
 for i in range(len(prio_services)):
  prio_services[i] = prio_services[i][2]
 return prio_services

def arrangeByType ( service_list , preferred_types ) : def enumerate ( elts ) : return zip ( range ( len ( elts ) ) , elts ) def bestMatchingService ( service ) : for ( i , t ) in enumerate ( preferred_types ) : if ( preferred_types [ i ] in service . type_uris ) : return i return len ( preferred_types ) prio_services = [ ( bestMatchingService ( s ) , orig_index , s ) for ( orig_index , s ) in enumerate ( service_list ) ] prio_services . sort ( ) for i in range ( len ( prio_services ) ) : prio_services [ i ] = prio_services [ i ] [ 2 ] return prio_services
rearrange service_list in a new list so services are ordered by types listed in preferred_types .