如何在Python中对此数据结构进行排序?

时间:2012-02-16 13:07:09

标签: python sorting dictionary pyyaml

我有一个数据结构(从YAML文档创建),如下所示:

{
    'SD_TX_EN_CFG[5]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 5)', 'Offset': 43024},
    'SD_TX_EN_CFG[4]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 4)', 'Offset': 40976},
    'SD_RX_EN_CFG[5]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 5)', 'Offset': 43008},
    'SD_RX_EN_CFG[3]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 3)', 'Offset': 38912},
    'SD_RX_EN_CFG[2]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 2)', 'Offset': 36864},
    'SD_RX_EN_CFG[0]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 0)', 'Offset': 32768}, 
    'aif2_scratch': {'Offset': 4, 'Bits': {'0-31': 'scratch'}, 'Description': None, 'Name': 'aif2_scratch'},
    'SD_RX_R2_CFG[0]': {'Bits': {'15-16': 'RXLOOPBACK // 00 = Disabled, 11 = Enabled'}, 'Name': 'RX Configuration Register 2 (Link 0)', 'Offset': 32776},
    'aif2_pid': {'Offset': 0, 'Bits': {'6-7': 'Custom version code', '8-10': 'Major revision X code // \n    Should be 1\n', '0-5': 'Minor revision Y code'}, 'Description': None, 'Name': 'aif2_pid'},
    'SD_TX_EN_CFG[1]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 1)', 'Offset': 34832},
    'SD_TX_EN_CFG[0]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 0)', 'Offset': 32784},
    'SD_RX_EN_CFG[1]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 1)', 'Offset': 34816},
    'SD_RX_EN_CFG[4]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 4)', 'Offset': 40960},
    'SD_TX_R1_CFG[0]': {'Bits': {'15-16': 'TXLOOPBACK // 00 = Disabled, 11 = Enabled'}, 'Name': 'TX Configuration Register 1 (Link 0)', 'Offset': 32788}, 
    'SD_TX_EN_CFG[2]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 2)', 'Offset': 36880},
    'RM_LK_CFG0[0]': {'Bits': {0: 'Short frame mode // 0 = CPRI, 1 = OBSAI', '2-3': 'Link Rate // 0 = 8x, 1 = 4x, 2 = 2x, 3 = 1x', 1: 'Enable RM Link // 0 = Disabled, 1 = Enabled'}, 'Name': 'RM Link Configuration Register 0 (Link 0)', 'Offset': 327680},
    'SD_TX_EN_CFG[3]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 3)', 'Offset': 38928}
}

YAML定义类似于

SD_RX_EN_CFG[0]:
  Name: RX Enable Register (Link 0)
  Offset: 0x8000
  Bits:
    0: ENRX // 0 = Disabled, 1 = Enabled

我想按照'Offset'定义的顺序处理每个元素,但我似乎无法做到这一点。我已经阅读并重新阅读了问题72899,但这看起来有点复杂。

4 个答案:

答案 0 :(得分:9)

for k, v in sorted(data.iteritems(), key=lambda (k,v): v['Offset']):
    print k, v['Offset']

打印:

aif2_pid 0
aif2_scratch 4
SD_RX_EN_CFG[0] 32768
SD_RX_R2_CFG[0] 32776
SD_TX_EN_CFG[0] 32784
SD_TX_R1_CFG[0] 32788
SD_RX_EN_CFG[1] 34816
SD_TX_EN_CFG[1] 34832
SD_RX_EN_CFG[2] 36864
SD_TX_EN_CFG[2] 36880
SD_RX_EN_CFG[3] 38912
SD_TX_EN_CFG[3] 38928
SD_RX_EN_CFG[4] 40960
SD_TX_EN_CFG[4] 40976
SD_RX_EN_CFG[5] 43008
SD_TX_EN_CFG[5] 43024
RM_LK_CFG0[0] 327680
您将使用Python 3.x中的

更新:(因为iteritemslambda更改):

for k, v in sorted(data.items(), key=lambda x: x[1]['Offset']):
    print k, v['Offset']

(这也适用于Python 2.x.)

答案 1 :(得分:2)

您可以使用key的{​​{1}}参数。假设sorted是您从YAML读取的词典:

ds

答案 2 :(得分:2)

data是您发布的词典:

sorted(data.items(), key=lambda i: i[1]['Offset'])

答案 3 :(得分:1)

以下是我如何使用它:

d={'SD_TX_EN_CFG[5]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 5)', 'Offset': 43024}, 'SD_TX_EN_CFG[4]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 4)', 'Offset': 40976}, 'SD_RX_EN_CFG[5]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 5)', 'Offset': 43008}, 'SD_RX_EN_CFG[3]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 3)', 'Offset': 38912}, 'SD_RX_EN_CFG[2]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 2)', 'Offset': 36864}, 'SD_RX_EN_CFG[0]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 0)', 'Offset': 32768}, 'aif2_scratch': {'Offset': 4, 'Bits': {'0-31': 'scratch'}, 'Description': None, 'Name': 'aif2_scratch'}, 'SD_RX_R2_CFG[0]': {'Bits': {'15-16': 'RXLOOPBACK // 00 = Disabled, 11 = Enabled'}, 'Name': 'RX Configuration Register 2 (Link 0)', 'Offset': 32776}, 'aif2_pid': {'Offset': 0, 'Bits': {'6-7': 'Custom version code', '8-10': 'Major revision X code // \n    Should be 1\n', '0-5': 'Minor revision Y code'}, 'Description': None, 'Name': 'aif2_pid'}, 'SD_TX_EN_CFG[1]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 1)', 'Offset': 34832}, 'SD_TX_EN_CFG[0]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 0)', 'Offset': 32784}, 'SD_RX_EN_CFG[1]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 1)', 'Offset': 34816}, 'SD_RX_EN_CFG[4]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 4)', 'Offset': 40960}, 'SD_TX_R1_CFG[0]': {'Bits': {'15-16': 'TXLOOPBACK // 00 = Disabled, 11 = Enabled'}, 'Name': 'TX Configuration Register 1 (Link 0)', 'Offset': 32788}, 'SD_TX_EN_CFG[2]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 2)', 'Offset': 36880}, 'RM_LK_CFG0[0]': {'Bits': {0: 'Short frame mode // 0 = CPRI, 1 = OBSAI', '2-3': 'Link Rate // 0 = 8x, 1 = 4x, 2 = 2x, 3 = 1x', 1: 'Enable RM Link // 0 = Disabled, 1 = Enabled'}, 'Name': 'RM Link Configuration Register 0 (Link 0)', 'Offset': 327680}, 'SD_TX_EN_CFG[3]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 3)', 'Offset': 38928}}
import pprint
pprint.pprint(d)

def f(i):
    return d[i]['Offset']

for i in d:
    print f(i)

_list=sorted(d,key=f)

def process(thing):
    pass

for k in _list:
    process(d[k])