Python Code Examples for sort by value

6 Python code examples are found related to "sort by value". These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Project: funcfinder   Author: alexmojaki   File: dict.py    License: MIT License 6 votes vote down vote up
def sort_dict_by_value(func):
    """
    Return a copy of a dict which still supports all the standard operations with the usual API,
    plus can be iterated over in sorted order by value. Adding keys to the dict may not necessarily preserve this order.
    """
    copy_dict(func)

    # On my machine at least, this dict does not look sorted by value
    original_dict = dict(zip(range(4), "dcba"))
    sorted_dict = func(original_dict)

    # Iteration is now ordered by value
    assertEqual(sorted_dict.items(), [(3, 'a'), (2, 'b'), (1, 'c'), (0, 'd')])

    # Larger test
    sorted_values = list(itertools.product(string.ascii_lowercase, string.ascii_lowercase))
    shuffled_values = list(sorted_values)
    for i in xrange(10):
        random.shuffle(shuffled_values)
        original_dict = dict(itertools.izip(itertools.count(), shuffled_values))
        sorted_dict = func(original_dict)
        assertEqualIters(sorted_values, sorted_dict.itervalues()) 
Example 2
Project: pynlp   Author: sliderSun   File: data_util_zhihu.py    License: MIT License 5 votes vote down vote up
def sort_by_value(d):
    items = d.items()
    backitems = [[v[1], v[0]] for v in items]
    backitems.sort(reverse=True)
    return [backitems[i][1] for i in range(0, len(backitems))] 
Example 3
Project: Tinder   Author: fbessez   File: features.py    License: MIT License 5 votes vote down vote up
def sort_by_value(sortType):
    '''
    Sort options are:
        'age', 'message_count', 'gender'
    '''
    global match_info
    return sorted(match_info.items(), key=lambda x: x[1][sortType], reverse=True) 
Example 4
Project: text_classification   Author: brightmart   File: data_util_zhihu.py    License: MIT License 5 votes vote down vote up
def sort_by_value(d):
    items=d.items()
    backitems=[[v[1],v[0]] for v in items]
    backitems.sort(reverse=True)
    return [ backitems[i][1] for i in range(0,len(backitems))] 
Example 5
Project: FACT_core   Author: fkie-cad   File: filter.py    License: GNU General Public License v3.0 5 votes vote down vote up
def sort_chart_list_by_value(input_data):
    try:
        input_data.sort(key=lambda x: x[1], reverse=True)
    except (AttributeError, IndexError, KeyError, TypeError):
        logging.error('Could not sort chart list {}'.format(input_data), exc_info=True)
        return []
    return input_data 
Example 6
Project: BioBlender   Author: MonZop   File: utilities.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def sortDictByValue(dict):
    """
        Sort a dictionary by its values

        Parameters
            dict:  The dictionary to sort (dict)
        Returns
            items: The dictionary sorted by value (list)
    """
    items = [(v, k) for k, v in dict.items()]
    items.sort()
    items.reverse()             
    items = [ k for v, k in items]
    return items