0

Its easy to sort a dict to list by values, but I need to order keys by kind of boosting specific values in correlation to others.

An example:

x = [('key1', {'s': 'foo', 'w': 30}), ('key2', {'s': 'bar', 'w': 26}),
     ('key3', {'s': 'foo', 'w': 23}), ('key4', {'s': 'bar', 'w': 13})]

result: ['key2', 'key1', 'key3', 'key4']

The stuff is ordered by 'w', but for 's' we prefer 'bar' over 'foo' if 'w' hits some treshold. Is this somehow implemented in python, are there any rules to do this or do you know a python library to handle it?

Its not about learning the features, its about ordering the way I specify - boost or restrict - the values.

rebeling
  • 718
  • 9
  • 31
  • Sort ‛w‛ then sort the slice of the resulting list by ‛bar‛, slicing where where the threshold is exceeded – YXD Feb 03 '13 at 20:29
  • So why is the result not key1, key2, key3, key4 (`w` from high to low)? – Simeon Visser Feb 03 '13 at 20:29
  • the w of 'key2' is 26 is smaller than 'key1' 30, but 'key2' is 'bar' and in this way ranked higher than 'foo' ...because the distance between 30 and 26 is smaller than between for 'key3' and 'key4' where it is 10. 10 is to much to still prefer 'bar' over 'foo' – rebeling Feb 04 '13 at 18:35

3 Answers3

2

In Python 2 you can use something along the line of this:

def compare(item1, item2):
    key1, it1 = item1
    key2, it2 = item2
    if max(it1['w'], it2['w']) > threshold:
        return cmp(it1['s'], it2['s'])
    else:
        return cmp(it1['w'], it2['w'])

and

sorted(x, cmp=compare)

sorted changed in python 3, if you use it see

http://code.activestate.com/recipes/576653-convert-a-cmp-function-to-a-key-function/

aseyboldt
  • 1,090
  • 8
  • 14
1

With that complex sorting needs you should look into key or cmp attributes of sorted(). See Python wiki for the details and examples: http://wiki.python.org/moin/HowTo/Sorting/#Key_Functions

Use key if importance can be determined based on a single element. If importance is dependent on relation between two elements you will be best using cmp.

Bartosz
  • 6,055
  • 3
  • 27
  • 17
0

No answer helped so far, but solution for me is:

Every key get a score at the beginning of 1.0 and then for every feature/value I multiply it with sth. and at the end I do a normal ordering.

key1['score'] is 1.0

# feature 1
if key['s'] == foo:
    score = score * 0.1  
else:
    score = score * 0.6

# feature 2
... and so on

order keys by score, done. 

Thx, for your questions, thoughts and comments.

rebeling
  • 718
  • 9
  • 31