私は、次のネストされた辞書があります。どのように入れ子になった辞書のpythonを並べ替えるには?
{'switch3': {'ip': '192.160.1.2', 'hostname': 'test-1'},
'switch2': {'ip': '192.160.1.8', 'hostname': 'test-1'},
'switch4': {'ip': '192.160.1.3', 'hostname': 'test-1'},
'switch1': {'ip': '192.160.1.4', 'hostname': 'test-1'}}
を、私はスイッチでソートし、この取得したい:
{'switch1': {'ip': '192.160.1.4', 'hostname': 'test-1'}
'switch2': {'ip': '192.160.1.8', 'hostname': 'test-1'},
'switch3': {'ip': '192.160.1.2', 'hostname': 'test-1'},
'switch4': {'ip': '192.160.1.3', 'hostname': 'test-1'}}
を、これは可能ですか?
辞書は並べ替えられていないため、**ソート**辞書はありません。 –
ディクショナリは(CPython 3.6より前に)順序付けされておらず、 'sorted()'を適用してから 'OrderedDict'に渡します。 –
' OrderedDict'はPython 3.6でも正しいコレクションです。実装に依存しないでください。 – Ryan