python - 使用sorted(dict.values(), ...)后打印字典

标签 python list sorting python-3.x dictionary

我正在尝试打印 Python 3.4 中的字典字典。该词典由正在解析需求文本文件的文本解析器填充。添加到字典中的每个需求(键)都以字典的形式具有与其关联的各种值,其中之一是数字。我正在尝试打印字典,以便键按其数值排序。这是我正在使用的代码:

import pprint
from operator import itemgetter
reqs = sorted(reqs.values(), key=itemgetter('number'))
pprint.pprint(reqs)

不幸的是,以这种方式使用sorted会导致print只打印字典的值,所以我最终得到了一个字典列表。自从我编写这段代码以来已经有几周了,但我记得搜索了很长一段时间并尝试了 sorted 的不同变体。这是第一个打印出按 number 值排序的值,但我现在意识到值字典缺少键。

我将此打印结果与打印下面没有sorted的值时发生的情况进行比较。您可以看到,通过sorted,值位于列表中并且缺少它们的键。

没有排序(是的,3、4、5 按顺序排列是巧合 - 它们是前三个打印的):

{'PROJ-UX-HLR-0003': {'category': 'UX',
                      'file': 'rm/app/parse/example_reqs.txt',
                      'level': 'HLR',
                      'number': '0003',
                      'prefix': 'PROJ',
                      'repo': 'repo',
                      'text': 'This SHALL be the 3rd requirement.'},
 'PROJ-UX-HLR-0004': {'category': 'UX',
                      'file': 'rm/app/parse/example_reqs.txt',
                      'level': 'HLR',
                      'number': '0004',
                      'prefix': 'PROJ',
                      'repo': 'repo',
                      'text': 'This SHALL be the 4th requirement.'},
 'PROJ-UX-HLR-0005': {'category': 'UX',
                      'file': 'rm/app/parse/example_reqs.txt',
                      'level': 'HLR',
                      'number': '0005',
                      'prefix': 'PROJ',
                      'repo': 'repo',
                      'text': 'This SHALL be the 5th requirement.'}, # etc.

经过排序:

[{'category': 'UX',
  'file': 'app/parse/example_reqs.txt',
  'level': 'SYS',
  'number': '0001',
  'prefix': 'PROJ',
  'repo': 'repo',
  'text': 'This SHALL be the 1st requirement.'},
 {'category': 'UX',
  'file': 'app/parse/example_reqs.txt',
  'level': 'SYS',
  'number': '0002',
  'prefix': 'PROJ',
  'repo': 'repo',
  'text': 'This SHALL be the 2nd requirement.'},
 {'category': 'UX',
  'file': 'app/parse/example_reqs.txt',
  'level': 'HLR',
  'number': '0003',
  'prefix': 'PROJ',
  'repo': 'repo',
  'text': 'This SHALL be the 3rd requirement.'}, # etc.

与此类似question ,但他的 key 仍然卡在身边,尽管在错误的地方。

任何有关打印完整的、排序的词典的帮助都将受到赞赏。

最佳答案

使用原始字典的排序项创建一个 OrderedDictionary

import operator, collections
number = operator.itemgetter('number')
item_one = operator.itemgetter(1)
def f(foo):
    return number(item_one(foo))
d = {'PROJ-UX-HLR-0005': {'category': 'UX',
                          'file': 'rm/app/parse/example_reqs.txt',
                          'level': 'HLR',
                          'number': '0005',
                          'prefix': 'PROJ',
                          'repo': 'repo',
                          'text': 'This SHALL be the 5th requirement.'},
     'PROJ-UX-HLR-0003': {'category': 'UX',
                          'file': 'rm/app/parse/example_reqs.txt',
                          'level': 'HLR',
                          'number': '0003',
                          'prefix': 'PROJ',
                          'repo': 'repo',
                          'text': 'This SHALL be the 3rd requirement.'},
     'PROJ-UX-HLR-0004': {'category': 'UX',
                          'file': 'rm/app/parse/example_reqs.txt',
                          'level': 'HLR',
                          'number': '0004',
                          'prefix': 'PROJ',
                          'repo': 'repo',
                          'text': 'This SHALL be the 4th requirement.'}
     }
ds = sorted(d.items(), key = f)
od = collections.OrderedDict(ds)

关于python - 使用sorted(dict.values(), ...)后打印字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26242482/

相关文章:

python - OSError [Errno 22] 在 Python 中使用 open() 时参数无效

python - 如果列表中的9个字符串全部被替换: do something

Python:如何使用特定版本 2.4.9 pip install opencv2?

python - Excel 文件损坏或扩展名错误错误 openpyxl & writerxlsx

python - 替换Python列表中的多个字符串值

php - 优化MYSQL+PHP父子输出

java - 随后按字段对对象进行排序

php - 以多级方法对数据进行排序,例如使用 PHP 和 MySql 在 Excel 中

python - 多处理:池和映射以及 sys.exit()

list - Scheme - 映射函数,用于将函数应用于嵌套列表中的元素