按数组python中的字典值排序

jdv*_*v12 1 python arrays sorting dictionary

好的,所以我一直在处理一些带注释的文本输出.到目前为止,我所拥有的是一个字典,其中注释为关键字,关系是一系列元素:

'Adenotonsillectomy': ['0', '18', '1869', '1716'],
 'OSAS': ['57', '61'],
 'apnea': ['41', '46'],
 'can': ['94', '97', '1796', '1746'],
 'deleterious': ['103', '114'],
 'effects': ['122', '129', '1806', '1752'],
 'for': ['19', '22'],
 'gain': ['82', '86', '1776', '1734'],
 'have': ['98', '102', ['1776 1786 1796 1806 1816'], '1702'],
 'health': ['115', '121'],
 'lead': ['67', '71', ['1869 1879 1889'], '1695'],
 'leading': ['135', '142', ['1842 1852'], '1709'],
 'may': ['63', '66', '1879', '1722'],
 'obesity': ['146', '153'],
 'obstructive': ['23', '34'],
 'sleep': ['35', '40'],
 'syndrome': ['47', '55'],
 'to': ['143', '145', '1852', '1770'],
 'weight': ['75', '81'],
 'when': ['130', '134', '1842', '1758'],
 'which': ['88', '93', '1786', '1740']}
Run Code Online (Sandbox Code Playgroud)

我想要做的是通过数组中的第一个元素对此进行排序,并将dict重新排序为:

'Adenotonsillectomy': ['0', '18', '1869', '1716']
'for': ['19', '22'],
'obstructive': ['23', '34'],
'sleep': ['35', '40'],
'apnea': ['41', '46'],
etc...
Run Code Online (Sandbox Code Playgroud)

现在我试图使用运算符按值排序:

sorted(dependency_dict.items(), key=lambda x: x[1][0])
Run Code Online (Sandbox Code Playgroud)

但是我得到的输出仍然不正确:

[('Adenotonsillectomy', ['0', '18', '1869', '1716']),
 ('deleterious', ['103', '114']),
 ('health', ['115', '121']),
 ('effects', ['122', '129', '1806', '1752']),
 ('when', ['130', '134', '1842', '1758']),
 ('leading', ['135', '142', ['1842 1852'], '1709']),
 ('to', ['143', '145', '1852', '1770']),
 ('obesity', ['146', '153']),
 ('for', ['19', '22']),
 ('obstructive', ['23', '34']),
 ('sleep', ['35', '40']),
 ('apnea', ['41', '46']),
 ('syndrome', ['47', '55']),
 ('OSAS', ['57', '61']),
 ('may', ['63', '66', '1879', '1722']),
 ('lead', ['67', '71', ['1869 1879 1889'], '1695']),
 ('weight', ['75', '81']),
 ('gain', ['82', '86', '1776', '1734']),
 ('which', ['88', '93', '1786', '1740']),
 ('can', ['94', '97', '1796', '1746']),
 ('have', ['98', '102', ['1776 1786 1796 1806 1816'], '1702'])]
Run Code Online (Sandbox Code Playgroud)

我不确定什么是错的.任何帮助表示赞赏.

Mob*_*erg 5

条目按字母顺序排序.如果要对整数值进行排序,请先将值转换为int:

sorted(dependency_dict.items(), key=lambda x: int(x[1][0]))
Run Code Online (Sandbox Code Playgroud)