so I have a dict :
marker_dict[experiment_id] = {'SM0012AQ': { 'chromosome' : 1, 'linkageGroup':8, 'positionCm' : 45,'qualityStatus' : 'A' }, 'SM0075AQ': { 'chromosome' : 1, 'linkageGroup': 7, 'positionCm' : ,'qualityStatus' : 'A' }, 'SM0078BQ': { 'chromosome' : 3, 'linkageGroup': 78, 'positionCm' : 7,'qualityStatus' : 'B' }, 'SM0079PQ': { 'chromosome' : 4, 'linkageGroup': , 'positionCm' : 80,'qualityStatus' : 'B' }, 'SM0080BQ': { 'chromosome' : , 'linkageGroup': 78, 'positionCm' : 447,'qualityStatus' : 'T' }}
I want to sort my dict on ‘i.chromosome’, ‘i.linkageGroup’ and ‘i.positionCm’
What I am doing :
marker_list = sorted(marker_dict[experiment_id].values(), key=lambda i: (i.chromosome, i.linkageGroup, i.positionCm))
And i got :
TypeError: '<' not supported between instances of 'Nonetype' and 'int'
In python 2, they were doing :
markerList=sorted(markerDic.values(), key=operator.attrgetter('chromosome','linkageGroup','positionCm'))
Could you help me please, I am lost !
Advertisement
Answer
Given
t = {'SM0012AQ': { 'chromosome' : 1, 'linkageGroup':8, 'positionCm' : 45,'qualityStatus' : 'A' }, 'SM0075AQ': { 'chromosome' : 1, 'linkageGroup': 7, 'positionCm' : None ,'qualityStatus' : 'A' }, 'SM0078BQ': { 'chromosome' : 3, 'linkageGroup': 78, 'positionCm' : 7,'qualityStatus' : 'B' }, 'SM0079PQ': { 'chromosome' : 4, 'linkageGroup': None, 'positionCm' : 80,'qualityStatus' : 'B' }, 'SM0080BQ': { 'chromosome' : None, 'linkageGroup': 78, 'positionCm' : 447,'qualityStatus' : 'T' }}
and the requirement that None
s are for int
, you can solve it using
dict(sorted(t.items(), key=lambda i: (i[1]['chromosome'] if i[1]['chromosome'] is not None else -1, i[1]['linkageGroup'] if i[1]['linkageGroup'] is not None else -1 , i[1]['positionCm'] if i[1]['positionCm'] is not None else -1)))
where I’ve made the assumption that the numbers you have are 0 or positive. If you can also have negatives you need to change the -1 to a very large negative number.
The result:
{'SM0080BQ': {'chromosome': None, 'linkageGroup': 78, 'positionCm': 447, 'qualityStatus': 'T'}, 'SM0075AQ': {'chromosome': 1, 'linkageGroup': 7, 'positionCm': None, 'qualityStatus': 'A'}, 'SM0012AQ': {'chromosome': 1, 'linkageGroup': 8, 'positionCm': 45, 'qualityStatus': 'A'}, 'SM0078BQ': {'chromosome': 3, 'linkageGroup': 78, 'positionCm': 7, 'qualityStatus': 'B'}, 'SM0079PQ': {'chromosome': 4, 'linkageGroup': None, 'positionCm': 80, 'qualityStatus': 'B'}}