简体   繁体   English

在python中按内部字典值排序后,嵌套字典的打印方式不同

[英]nested Dictionary print differently after sorting by inner dictionary values in python

I was trying sorting nested dict by inner dict's value.我正在尝试按内部字典的值对嵌套字典进行排序。 The sorting went well.排序进行得很顺利。 but when I check my result, I found out that the original dict was printed when I just use the variable (d2), but it gives me the correct result when I use print(d2)但是当我检查我的结果时,我发现当我只使用变量(d2)时打印了原始dict,但是当我使用print(d2)时它给了我正确的结果

d2 = {1: {1: 4, 2: 5, 3: 6}, 
      2: {7: 13, 8: 14, 9: 15, 10: 16, 11: 17, 12: 18},
      3: {1: 1, 2: 9, 3: 4}}

# sorting by inner dict value
for keys in d2.keys():
  sorted_tuples = sorted(d2[keys].items(), key=operator.itemgetter(1), reverse=True)
  d2[keys] = {k: v for k, v in sorted_tuples}

print(d2)
d2

{1: {3: 6, 2: 5, 1: 4}, 2: {12: 18, 11: 17, 10: 16, 9: 15, 8: 14, 7: 13}, 3: {2: 9, 3: 4, 1: 1}}
{1: {1: 4, 2: 5, 3: 6},
 2: {7: 13, 8: 14, 9: 15, 10: 16, 11: 17, 12: 18},
 3: {1: 1, 2: 9, 3: 4}}

why the output is different when I use d2 and print(d2)为什么当我使用 d2 和 print(d2) 时输出不同

friend!朋友! Did you use the pretty print module to print the results of d2 ?您是否使用过漂亮的打印模块来打印d2的结果? I was only able to replicate your behavior using the pretty print module.我只能使用漂亮的打印模块复制您的行为。 Pretty print alphabetically sorts a dictionary before printing it, which can be disabled . Pretty print 在打印字典之前按字母顺序对字典进行排序,这可以禁用

I originally (and wrongly) suspected the different output between d2 and print(d2) was a result of dictionaries being unordered collections of data;我最初(并且错误地)怀疑d2print(d2)之间的不同输出是字典是无序数据集合的结果; I suspected dict.__str__ and dict.__repr__ differed just enough.我怀疑dict.__str__dict.__repr__不同。 I would recommend you use an OrderedDict over a standard dictionary if you wish to maintain its order-- despite Python preserving dictionaries insertion order in Python 3.7 .如果您希望保持其顺序,我建议您在标准字典上使用OrderedDict 尽管 Python 保留了 Python 3.7 中的字典插入顺序

Below is my code and conclusions.下面是我的代码和结论。

After initialization, d2 and print(d2) printed the same values:初始化后, d2print(d2)打印出相同的值:

❯ python
Python 3.7.12 (default, Sep 10 2021, 17:29:55) 
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> d2 = {1: {1: 4, 2: 5, 3: 6}, 
      2: {7: 13, 8: 14, 9: 15, 10: 16, 11: 17, 12: 18},
      3: {1: 1, 2: 9, 3: 4}}
>>> d2
{1: {1: 4, 2: 5, 3: 6}, 2: {7: 13, 8: 14, 9: 15, 10: 16, 11: 17, 12: 18}, 3: {1: 1, 2: 9, 3: 4}}
>>> print(d2)
{1: {1: 4, 2: 5, 3: 6}, 2: {7: 13, 8: 14, 9: 15, 10: 16, 11: 17, 12: 18}, 3: {1: 1, 2: 9, 3: 4}}

After sorting, d2 and print(d2) printed the same values.排序后, d2print(d2)打印出相同的值。

>>> import operator
>>> for keys in d2.keys():
...  sorted_tuples = sorted(d2[keys].items(), key=operator.itemgetter(1), reverse=True)
...  d2[keys] = {k: v for k, v in sorted_tuples}
... 
>>> print(d2)
{1: {3: 6, 2: 5, 1: 4}, 2: {12: 18, 11: 17, 10: 16, 9: 15, 8: 14, 7: 13}, 3: {2: 9, 3: 4, 1: 1}}
>>> d2
{1: {3: 6, 2: 5, 1: 4}, 2: {12: 18, 11: 17, 10: 16, 9: 15, 8: 14, 7: 13}, 3: {2: 9, 3: 4, 1: 1}}

However, while using the pretty print module, I was able to replicate your behavior.但是,在使用漂亮的打印模块时,我能够复制您的行为。

>>> from pprint import pprint as pp
>>> pp(print(d2))
{1: {3: 6, 2: 5, 1: 4}, 2: {12: 18, 11: 17, 10: 16, 9: 15, 8: 14, 7: 13}, 3: {2: 9, 3: 4, 1: 1}}
>>> pp(d2)
{1: {1: 4, 2: 5, 3: 6},
 2: {7: 13, 8: 14, 9: 15, 10: 16, 11: 17, 12: 18},
 3: {1: 1, 2: 9, 3: 4}}

Once I disabled dictionary sorting in the pretty print module, I was able to obtain your desired output.一旦我在漂亮的打印模块中禁用了字典排序,我就能够获得您想要的输出。

>>> pprint.sorted = lambda x, key=None: x
>>> pp(d2)
{1: {3: 6, 2: 5, 1: 4},
 2: {12: 18, 11: 17, 10: 16, 9: 15, 8: 14, 7: 13},
 3: {2: 9, 3: 4, 1: 1}}
>>> pp(print(d2))
{1: {3: 6, 2: 5, 1: 4}, 2: {12: 18, 11: 17, 10: 16, 9: 15, 8: 14, 7: 13}, 3: {2: 9, 3: 4, 1: 1}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM