如何在Python中按字典值正确对字典排序?

时间:2019-03-23 07:21:39

标签: python

鉴于下面的字典,我试图按值顺序0,1,2,3,4对它进行排序,但没有成功

items = {
            'Customer'          : ['Form', 'Customer is module for recording information.', 0], 
            'Age'               : ['Label', 'This field for input Age of Customer automatically calculated by system against date of birth.', 4], 
            'Salutation'        : ['Label', 'A greeting in words or actions or the words used at the beginning of a letter or speech.', 2], 
            'As a Guarantor'    : ['Label', 'Whether or not a customer is a guarantor to another customer.', 1], 
            'First Name English': ['Label', 'The name that was given to you when you were born and that comes before your family name.', 3]
        }

print "before sort dictionary"
print items

items    = dict(sorted(items.items(),key=lambda (k,v):int(v[2]))) # Sort Dictionary By key/value 'Order'
print "after sort dictionary"
print items

sorted()之后,字典略有变化,但仍然不正确,排序后的项目如下:

{
    'Customer'          : ['Form', 'Customer is module for recording information.', 0], 
    'As a Guarantor'    : ['Label', 'Whether or not a customer is a guarantor to another customer.', 1], 
    'Age'               : ['Label', 'This field for input Age of Customer automatically calculated by system against date of birth.', 4], 
    'Salutation'        : ['Label', 'A greeting in words or actions or the words used at the beginning of a letter or speech.', 2], 
    'First Name English': ['Label', 'The name that was given to you when you were born and that comes before your family name.', 3]
}

请帮助该词典如何正确排序?谢谢

0 个答案:

没有答案