Example #1
0
def multi_key_sort(items, columns):
    comparators = [((i(col[1:].strip()), -1) if col.startswith('-') else (i(col.strip()), 1)) for col in columns]

    def comparator(left, right):
        comparator_iter = (cmp(fn(left), fn(right)) * multi for fn, multi in comparators)
        return next((result for result in comparator_iter if result), 0)

    return sorted(items, key=cmp_to_key(comparator))
Example #2
0
def multikeysort(items, columns):
    comparers = [((i(col[1:].strip()), -1) if col.startswith('-') else
                  (i(col.strip()), 1)) for col in columns]

    def comparer(left, right):
        comparer_iter = (cmp(fn(left), fn(right)) * mult
                         for fn, mult in comparers)
        return next((result for result in comparer_iter if result), 0)

    return sorted(items, key=cmp_to_key(comparer))
Example #3
0
def multikeysort(items, columns):
    """http://stackoverflow.com/a/1144405"""
    from operator import itemgetter as i
    from functools import cmp_to_key
    comparers = [((i(col[1:].strip()), -1) if col.startswith('-') else
                  (i(col.strip()), 1)) for col in columns]

    def comparer(left, right):
        comparer_iter = (cmp(fn(left), fn(right)) * mult
                         for fn, mult in comparers)
        return next((result for result in comparer_iter if result), 0)

    return sorted(items, key=cmp_to_key(comparer))
Example #4
0
def multikeysort(items, columns):
    """http://stackoverflow.com/a/1144405"""
    from operator import itemgetter as i
    from functools import cmp_to_key
    comparers = [
        ((i(col[1:].strip()), -1) if col.startswith('-') else (i(col.strip()), 1))
        for col in columns
    ]
    def comparer(left, right):
        comparer_iter = (
            cmp(fn(left), fn(right)) * mult
            for fn, mult in comparers
        )
        return next((result for result in comparer_iter if result), 0)
    return sorted(items, key=cmp_to_key(comparer))
Example #5
0
def multikeysort(items, columns):
    """
    Given an iterable of dicts, sort it by the columns (keys) specified in `columns` in order they appear.
    c.f. https://stackoverflow.com/questions/1143671/python-sorting-list-of-dictionaries-by-multiple-keys
    """

    # cmp was builtin in python2, have to add it for python3
    def cmp(a, b):
        return (a > b) - (a < b)

    comparers = [((i(col[1:].strip()), -1) if col.startswith("-") else
                  (i(col.strip()), 1)) for col in columns]

    def comparer(left, right):
        comparer_iter = (cmp(fn(left), fn(right)) * mult
                         for fn, mult in comparers)
        return next((result for result in comparer_iter if result), 0)

    return sorted(items, key=cmp_to_key(comparer))
Example #6
0
    def get_sorted_arr_from_obj(wrestler_obj: dict, columns: list) -> list:
        wrestler_arr = []
        for wrestler, data in wrestler_obj.items():
            obj = {'wrestler': wrestler}
            for k, v in data.items():
                obj[k] = v
            wrestler_arr.append(obj)

        comparers = [
            ((i(col[1:].strip()), -1) if col.startswith('-') else (i(col.strip()), 1))
            for col in columns
        ]

        def cmp(a, b):
            return (a > b) - (a < b)

        def comparer(left, right):
            comparer_iter = (
                cmp(fn(left), fn(right)) * mult
                for fn, mult in comparers
            )
            return next((result for result in comparer_iter if result), 0)
        return sorted(wrestler_arr, key=cmp_to_key(comparer))
# Crie um programa onde 4 jogadores joguem um dado e tenham resultados aleatórios. Guarde esses resultados em um
# dicionário. No final, coloque esse dicionário em ordem, sabendo que o vencedor tirou o maior número no dado.

from random import randint as r
from operator import itemgetter as i

jogo = {}
ordem = {}
for c in range(1, 5):
    jogo[f'Jogador {c}'] = r(1, 6)
for k, v in jogo.items():
    print(f'{k}: {v}')
ordem = sorted(jogo.items(), key=i(1), reverse=True)
print()
print('RANKING')
for v, d in enumerate(ordem):
    print(f'{v + 1}° lugar: {d[0]} com valor {d[1]}')