-6

私は次のようなPython辞書を持っています:

DictValue = {
'Event1': {},
'Event2': {
    'copy3': {
        'SourcePath': 'souce3',
        'DestPath': 'dest3',
        'Platform': u 'Debug|Win32'
    },
    'copy2': {
        'SourcePath': 'souce3',
        'DestPath': 'dest2',
        'Platform': u 'Debug|Win32'
    },
    'copy1': {
        'SourcePath': 'souce1',
        'DestPath': 'dest1',
        'Platform': u 'Debug|x64'
    },
    'copy5': {
        'SourcePath': 'souce5',
        'DestPath': 'dest5',
        'Platform': u 'Release|Win32'
    },
    'copy4': {
        'SourcePath': 'souce4',
        'DestPath': 'dest4',
        'Platform': u 'Release|x64'
    }
}
}

辞書の値を次のように並べ替えたい

{
'Event1': {},
'Event2': {
    'copy1': {
        'SourcePath': 'souce1',
        'DestPath': 'dest1',
        'Platform': u 'Debug|Win32'
    },
    'copy2': {
        'SourcePath': 'souce2',
        'DestPath': 'dest2',
        'Platform': u 'Debug|Win32'
    },
    'copy3': {
        'SourcePath': 'souce3',
        'DestPath': 'dest3',
        'Platform': u 'Debug|x64'
    },
    'copy4': {
        'SourcePath': 'souce4',
        'DestPath': 'dest4',
        'Platform': u 'Release|Win32'
    },
    'copy5': {
        'SourcePath': 'souce5',
        'DestPath': 'dest5',
        'Platform': u 'Release|x64'
    }
}
​}

出来ますか?助けてください。使ってますpython 2.6

ありがとう。

4

1 に答える 1

4

辞書には順序がありません。注文が必要な場合のオプションは次のとおりです。

  • 代わりにタプルのリストを使用します。
  • .keys()ループするときの並べ替え(.values()または.items()、必要に応じて)
  • 代わりにOrderedDictを使用してください。
于 2012-08-08T13:14:26.230 に答える