python字典顺序排列,按字母顺序排列字典(Python)

I am using a Python dictionary (product_dict) to represent a hierarchy of a product and all its subparts. The dict's keys are unique IDs (UUID), and the values are Class objects containing all information about those parts, including:

part.name # A string, containing the actual name of a component

part.idCode # UUID of component

part.parent # UUID of parent component

part.children # List of UUIDs of child components

part.tier # An integer that specifies its tier/level within the hierarchy

Now for the purpose of outputting the data in an orderly fashion, I wish to sort the parts both hierarchically and alphabetically. For hierarchical sorting using a tree structure, I have found the answer to this question to work great for printing: Sorting data hierarchically. For this example to work with my data structure, I have made some slight modifications:

class Node:

def __init__(self, article):

self.article = article

self.children = []

self.parent = None

self.name = None

def printer(self, level=0):

print ('{}{}'.format('\t' * level, self.name))

for child in self.children:

child.printer(level + 1)

class Tree:

def __init__(self):

self.nodes = {}

def push(self, article, parent, name):

if parent not in self.nodes:

self.nodes[parent] = Node(parent)

if article not in self.nodes:

self.nodes[article] = Node(article)

if parent == article:

return

self.nodes[article].name = name

self.nodes[article].parent = self.nodes[parent]

self.nodes[parent].children.append(self.nodes[article])

@property

def roots(self):

return (x for x in self.nodes.values() if not x.parent)

t = Tree()

for idCode, part in product_dict.iteritems():

t.push(idCode, part.parent, part.name)

for node in t.roots:

node.printer()

Considering an example of my product being an aircraft, the output now looks as follows (the actual order varies):

Aircraft

Systems

Subsystem 2

Subsystem 1

Subsubsystem 1.1

Engines

Airframe

Section 2

Section 1

Section 4

Section 3

However, due to my limited understanding of Python at this stage, I am struggling to add alphabetical sorting to this routine (based on the part.name strings). I understand how the tree is built up, but I don't grasp the printing routine, and therefore fail to judge where to add a alphabetic sorting routine.

With the given example, my desired output should be:

Aircraft

Airframe

Section 1

Section 2

Section 3

Section 4

Engines

Systems

Subsystem 1

Subsubsystem 1.1

Subsystem 2

Any help is greatly appreciated. I do not adhere to the hierarchical sorting method given above, so I am open to entirely different approaches.

解决方案

It seems like the only thing you need to do is sort the children by name, when printing, i.e.

for child in sorted(self.children, key = lambda x: x.name):

child.printer(level + 1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值