python - 按多个元素按升序和降序对元组列表进行排序

标签 python python-3.x sorting

我有一个很大的元组列表,遵循以下结构:

('global_access', '2395', 'SQA', 'e69e1c69', '0', '/home/daoxley/git_stuff/sqa/sqa.yaml', '/home/daoxley/git_stuff/sqa/', '1.57.0')
('global_access', '2320', 'SQA', '7d1290cc', '0', '/home/daoxley/git_stuff/sqa/sqa.yaml', '/home/daoxley/git_stuff/sqa/', '1.57.0-7-g7d1290c')
('global_access', '2281', 'SQA', 'ead134e7', '0', '/home/daoxley/git_stuff/sqa/sqa.yaml', '/home/daoxley/git_stuff/sqa/', '1.57.0-9-gead134e')
('global_access', '2230', 'SQA', '8e3404b3', '0', '/home/daoxley/git_stuff/sqa/sqa.yaml', '/home/daoxley/git_stuff/sqa/', '1.57.0-12-g8e3404b')
('global_access', '2158', 'SQA', 'b19ba1fa', '0', '/home/daoxley/git_stuff/sqa/sqa.yaml', '/home/daoxley/git_stuff/sqa/', '1.57.0-15-gb19ba1f')
('global_access', '2345', 'SQA', '03fbe13d', '0', '/home/daoxley/git_stuff/sqa/sqa.yaml', '/home/daoxley/git_stuff/sqa/', '1.58.0')

我试图根据内部元素对这些数据进行排序,其中一些按升序排列,另一些按降序排列。我想要的顺序是第三个(升序)、第六个(升序)、第七个(升序)、第八个(降序)和第四个(降序)。

我已经完成了大部分工作,但我不知道如何按降序排列第 8 个元素,以便数据看起来像这样:

('global_access', '2345', 'SQA', '03fbe13d', '0', '/home/daoxley/git_stuff/sqa/sqa.yaml', '/home/daoxley/git_stuff/sqa/', '1.58.0')
('global_access', '2158', 'SQA', 'b19ba1fa', '0', '/home/daoxley/git_stuff/sqa/sqa.yaml', '/home/daoxley/git_stuff/sqa/', '1.57.0-15-gb19ba1f')
('global_access', '2230', 'SQA', '8e3404b3', '0', '/home/daoxley/git_stuff/sqa/sqa.yaml', '/home/daoxley/git_stuff/sqa/', '1.57.0-12-g8e3404b')
('global_access', '2281', 'SQA', 'ead134e7', '0', '/home/daoxley/git_stuff/sqa/sqa.yaml', '/home/daoxley/git_stuff/sqa/', '1.57.0-9-gead134e')
('global_access', '2320', 'SQA', '7d1290cc', '0', '/home/daoxley/git_stuff/sqa/sqa.yaml', '/home/daoxley/git_stuff/sqa/', '1.57.0-7-g7d1290c')
('global_access', '2395', 'SQA', 'e69e1c69', '0', '/home/daoxley/git_stuff/sqa/sqa.yaml', '/home/daoxley/git_stuff/sqa/', '1.57.0')

目前我的排序代码如下所示:

rule_list_to_check.sort(key=lambda x:  (x[2].lower(), x[5], x[6], LooseVersion(x[7])), -int(x[4]))

我尝试使用反向,如下所示,但这给了我以下错误:

rule_list_to_check.sort(key=lambda x:  (x[2].lower(), x[5], x[6], LooseVersion(x[7]), -int(x[4]),reverse=(False, False, False, True, False ))

并且尝试将 - 添加到 LooseVersion(x[7]) 也会出错

最佳答案

我将定义一个显式比较函数,然后使用 functools.cmp_to_key

from operator import itemgetter

# O compare, how I miss thee
def compare(x, y):
    if x < y:
        return -1
    elif x == y:
        return 0
    else:  # x > y
        return 1


# Comparing modified versions of the elements
def compare_on(x, y, f=None):
    if f is not None:
        x = f(x)
        y = f(y)
    return compare(x, y)


def compare_tuples(t1, t2):
    # Compare the first three fields in ascending order,
    # but the last two fields in descending order
    return (compare_on(t1, t2, itemgetter(2, 5, 6)) or
            compare_on(t2, t1, itemgetter(7, 3))
    # NOTE that t1 and t2 are swapped between the two calls to compare


rule_list_to_check.sort(key=functools.cmp_to_key(compare_tuples))

关于python - 按多个元素按升序和降序对元组列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59630453/

相关文章:

python-3.x - 捕获python3中子进程的所有输出

python - 在Python中解压字典列表

jquery - 使用 DataTable 或 jQuery 禁用对空列的排序

php - MySQL : random sort than sort by a specific column

python - 如何使用 virtualenv 在 django 中启动一个新项目

python - Numpy 一次将数组与多个标量进行比较

python-3.x - pip 卸载 : "No files were found to uninstall."

python - 如何用keras显示多张图像?

python - 在 python 中的三个重叠集中查找增量

Java - 对字符串列表进行排序,根据字符串的包含确定顺序