python - 如何先按键排序,然后按嵌套值排序

标签 python dictionary

考虑以下结构

 'source-document01013.txt': {'AAAGCTTACA': {'endPos': '141',
                                             'startPos': '132'},
                              'AAATCTTAGA': {'endPos': '105',
                                             'startPos': '96'},
                              'AAATGTCCCC': {'endPos': '75',
                                             'startPos': '66'}
                             }

我想按 'source-document01013.txt' 排序,然后按 startPos 排序,我该怎么做?

我之前有下面的行,但现在我添加了一个嵌套字典,这打破了。

newDict = Sorted(dictionary.items(), key = lambda x: (x[1], int(x[1]['startPos'])))

错误:IndexError:元组索引超出范围

最佳答案

l = []
for k, v in sorted(dictionary.items()):
    l.append((k, sorted(v.items(), key=lambda x: int(x[1]['startPos']))))

关于python - 如何先按键排序,然后按嵌套值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36312504/

相关文章:

python - 将列表中的元组转换为 int

Python 支持排序字典——类似于 C++ 映射?

java - 请解释通过这种方式检索时的列表功能

c# - 字符串数组查找字典

arrays - 从 CoreData 获取给定属性具有相同值的对象?

python - 使用自己的密码字典加密

python - 在 unixen 上编写全屏脚本、curses 风格、更新表格输出(a la top)

python - Python中的按键错误4

python - 在 python 脚本中运行我想要的所有包的方法

python - struct.pack(!i, 10) 结果在 Windows 上是 "00 00 00 0d 0a"