2 回答
TA贡献1886条经验 获得超2个赞
我d以您的命令为准,并通过sorted和解决了lambda
# Sort with chain_id
In [93]: sort_1 = sorted(d['mappings'],key=lambda x:x['chain_id'])
# Sort with start residue number
In [94]: sort_2 = sorted(sort_1,key=lambda x:x['start']['residue_number'])
# Sort with end residue number
In [95]: sort_3 = sorted(sort_2,key=lambda x:x['end']['residue_number'])
或者您可以排成一行。
sorted(d['mappings'],key=lambda x:(x['chain_id'],x['start']['residue_number'],x['end']['residue_number']))
灵感来自 leotrubach 的回答。
TA贡献1816条经验 获得超4个赞
如果所有键都按降序排序,您可以使用list.sort()或sorted()函数并创建一个返回键函数的元组:
print(
sorted(
mappings,
key=lambda x: (
x['chain_id'],
x['start']['residue_number'],
x['end']['residue_number']
)
)
)
添加回答
举报