【问题标题】:Sorting of list which contains dictionary in Python3Python3中包含字典的列表的排序
【发布时间】:2020-10-31 01:22:58
【问题描述】:

我有 2 个非常嵌套的 Python 字典,我想比较它们(我真正的 Json 文件包含数十万行)。这些字典包含列表,而这些列表包含字典。元素的顺序不是固定的,在字典的情况下不是问题,但在列表的情况下是问题。所以我必须对结构中的元素进行排序。

我编写了一个排序算法,它对我的​​数据结构中的项目进行递归排序。

我的代码在 Python2.7 可执行文件中按预期工作,但它不适用于 Python3.6.6 可执行文件。

我已经阅读了Python官方文档,知道list.sort()在Python2和Python3之间已经发生了变化,但我觉得这在Python3中是一个很大的限制。

我熟悉key 参数,但它不能解决我的问题。此外,字典中的键也不相同。

所以,我的问题是:是否可以对包含更多类型元素的列表进行排序?

代码:

test_1 = {"aaa": 111, "bbb": 222, "ccc": [{"o": [1, "t"]}, "a", "b", 1, [1, 2, [4, 3, [6, 5]]]]}
test_2 = {"bbb": 222, "aaa": 111, "ccc": [[2, 1, [3, 4, [5, 6]]], 1, "a", "b", {"o": ["t", 1]}]}


def list_sort(l):
    if isinstance(l, list):
        l.sort()
        for x in l:
            list_sort(x)


def dict_sorter(d):
    for k, v in d.items():
        if isinstance(v, dict):
            dict_sorter(v)
        elif isinstance(v, list):
            v.sort()
            for x in v:
                if isinstance(x, dict):
                    dict_sorter(x)
                elif isinstance(x, list):
                    list_sort(x)


print("\n\nBEFORE:")
print(test_1)
print(test_2)
print("EQ: {}".format(test_1 == test_2))

dict_sorter(test_1)
dict_sorter(test_2)

print("\n\nAFTER:")
print(test_1)
print(test_2)
print("EQ: {}".format(test_1 == test_2))

使用 Python2 输出:

>>> python2 test.py

BEFORE:
{'aaa': 111, 'bbb': 222, 'ccc': [{'o': [1, 't']}, 'a', 'b', 1, [1, 2, [4, 3, [6, 5]]]]}
{'aaa': 111, 'bbb': 222, 'ccc': [[2, 1, [3, 4, [5, 6]]], 1, 'a', 'b', {'o': ['t', 1]}]}
EQ: False

AFTER:
{'aaa': 111, 'bbb': 222, 'ccc': [1, {'o': [1, 't']}, [1, 2, [3, 4, [5, 6]]], 'a', 'b']}
{'aaa': 111, 'bbb': 222, 'ccc': [1, {'o': [1, 't']}, [1, 2, [3, 4, [5, 6]]], 'a', 'b']}
EQ: True

使用 Python3 输出:

>>> python3 test.py

BEFORE:
{'aaa': 111, 'bbb': 222, 'ccc': [{'o': [1, 't']}, 'a', 'b', 1, [1, 2, [4, 3, [6, 5]]]]}
{'bbb': 222, 'aaa': 111, 'ccc': [[2, 1, [3, 4, [5, 6]]], 1, 'a', 'b', {'o': ['t', 1]}]}
EQ: False
Traceback (most recent call last):
  File "test.py", line 30, in <module>
    dict_sorter(test_1)
  File "test.py", line 17, in dict_sorter
    v.sort()
TypeError: '<' not supported between instances of 'str' and 'dict'

【问题讨论】:

  • 这将help1 how to sorthelp2 sort different type of object,我个人认为,当您遇到具有多种数据类型的列表时,您需要设置数据类型的优先级顺序,如果对父列表进行排序,则需要设置,只需对内部对象数据类型进行单独排序并按顺序排列它们,其次(不完全确定)如果可能的话尝试创建一个与 python2.7 中使用的相同的排序类,并借助它解决这个问题
  • 感谢您的回答。我已经阅读了这些文档,但它们并没有解决我的问题。也许我可以编写一个排序类,但它会非常复杂并且需要付出很大的努力(分离类型并对它们进行排序并再次构建结构等......)。希望对这种排序有更简单有效的解决方案。
  • 我认为这都是因为python2.7和python3的实现差异,尝试numpy,看看它是否有帮助,因为我不太熟悉它所以不能说太多
  • @sahasrara62,当然,我在问题中提到我知道 Python2/Python3 的差异。

标签: python python-3.x list sorting dictionary


【解决方案1】:

在 python2 中,可以将任何数据与任何其他数据进行比较。但是,在 python3 中这是不可能的。

[参考]

https://portingguide.readthedocs.io/en/latest/comparisons.html

【讨论】:

  • 感谢您的回答,但正如我所提到的,我已经阅读了 Python 文档,并且知道 Python2/3 之间的差异。我觉得这种差异在 Python3 中是一个很大的限制。我对此问题的解决方案或“破解”感兴趣。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多