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.
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/
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
.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.