简体   繁体   中英

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 = {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)

friend! Did you use the pretty print module to print the results of 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 .

I originally (and wrongly) suspected the different output between d2 and print(d2) was a result of dictionaries being unordered collections of data; I suspected dict.__str__ and dict.__repr__ differed just enough. 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 .

Below is my code and conclusions.

After initialization, d2 and print(d2) printed the same values:

❯ 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.

>>> 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}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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