Python演算法-How to sort a dictionary by value
The other day I was asked if there was a way to sort a dictionary by value. If you use Python regularly, then you know that the dictionary data structure is by definition an unsorted mapping type. Some would define a dict as a hash table. Regardless, I needed a way to sort a nested dictionary (i.e. a dictionary of dictionaries) based on a value in the nested dictionaries so I could iterate over the keys in the specified order. We’ll spend some time looking at an implementation I found.
After Googling for ideas, I came across an answer on StackOverflow that did most of what I wanted. I had to modify it slightly to make it sort using my nested dictionary values though, but that was surprisingly easy. Before we get to the answer, we should take a quick look at the data structure. Here is a variation of the beast minus the private parts that were removed for your safety:
{'CLAIMID': '123456789',
'CLAIMDATE': '20120508',
'AMOUNT': '365.64', 'EXPDATE': '20120831'},
'fe614868-d0c0-4c62-ae02-7737dea82dba':
{'CLAIMID': '45689654',
'CLAIMDATE': '20120508',
'AMOUNT': '185.55', 'EXPDATE': '20120831'},
'ca1aa579-a9e7-4ade-80a3-0de8af4bcb21':
{'CLAIMID': '98754651',
'CLAIMDATE': '20120508',
'AMOUNT': '93.00', 'EXPDATE': '20120831'},
'ccb8641f-c1bd-45be-8f5e-e39b3be2e0e3':
{'CLAIMID': '789464321',
'CLAIMDATE': '20120508', 'AMOUNT': '0.00',
'EXPDATE': ''},
'e1c445c2-5148-4a08-9b7e-ff5ed51c43ed':
{'CLAIMID': '897987945',
'CLAIMDATE': '20120508',
'AMOUNT': '62.66', 'EXPDATE': '20120831'},
'77ad6dd4-5704-4060-9c38-6a93721ef98e':
{'CLAIMID': '23212315',
'CLAIMDATE': '20120508',
'AMOUNT': '41.05', 'EXPDATE': '20120831'}
}Now we know what we’re dealing with. Let’s take a quick look at the slightly modified answer I came up with:
After Googling for ideas, I came across an answer on StackOverflow that did most of what I wanted. I had to modify it slightly to make it sort using my nested dictionary values though, but that was surprisingly easy. Before we get to the answer, we should take a quick look at the data structure. Here is a variation of the beast minus the private parts that were removed for your safety:
CODE:
mydict = {'0d6f4012-16b4-4192-a854-fe9447b3f5cb':{'CLAIMID': '123456789',
'CLAIMDATE': '20120508',
'AMOUNT': '365.64', 'EXPDATE': '20120831'},
'fe614868-d0c0-4c62-ae02-7737dea82dba':
{'CLAIMID': '45689654',
'CLAIMDATE': '20120508',
'AMOUNT': '185.55', 'EXPDATE': '20120831'},
'ca1aa579-a9e7-4ade-80a3-0de8af4bcb21':
{'CLAIMID': '98754651',
'CLAIMDATE': '20120508',
'AMOUNT': '93.00', 'EXPDATE': '20120831'},
'ccb8641f-c1bd-45be-8f5e-e39b3be2e0e3':
{'CLAIMID': '789464321',
'CLAIMDATE': '20120508', 'AMOUNT': '0.00',
'EXPDATE': ''},
'e1c445c2-5148-4a08-9b7e-ff5ed51c43ed':
{'CLAIMID': '897987945',
'CLAIMDATE': '20120508',
'AMOUNT': '62.66', 'EXPDATE': '20120831'},
'77ad6dd4-5704-4060-9c38-6a93721ef98e':
{'CLAIMID': '23212315',
'CLAIMDATE': '20120508',
'AMOUNT': '41.05', 'EXPDATE': '20120831'}
}Now we know what we’re dealing with. Let’s take a quick look at the slightly modified answer I came up with:
CODE:
sorted_keys = sorted(mydict.keys(), key=lambda y: (mydict[y]['CLAIMID']))來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-733543/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python dict sort排序 按照key,valuePython排序
- How Oracle Uses the Data Dictionary (262)Oracle
- Python, pandas: how to sort dataframe by index// Merge two dataframes by indexPythonIndex
- How to get ORACLE_HOME from data dictionaryOracle
- C# 遍歷Dictionary並修改其中的ValueC#
- Algorithm-sort 排序演算法 pythonGo排序演算法Python
- how to get sharepoint lookup value
- How to inject value into bean properties in SpringBeanSpring
- Python 3 字典(Dictionary)Python
- How to update BOL entity property value via ABAP code
- golang sort.Sort () 排序演算法學習Golang排序演算法
- school dictionary, kids dictionary, children dictionary
- Dictionary key 根據ASCII排序並拼接key和value並HMACMD5-iOSASCII排序MacACMiOS
- python中的list、tuple和dictionaryPython
- 快速排序演算法(Quick_Sort)排序演算法UI
- python比較json/dictionary的庫PythonJSON
- How Python list works?Python
- How to convert a numeric value or currency to English words using C#C#
- Python中sort()方法的使用Python
- 排序演算法-氣泡排序(Bubble Sort)排序演算法
- python第三天(list,元組,dictionary)Python
- How to install Python module?Python
- Python 排序---sort與sorted學習Python排序
- Arrays.Sort()中的那些排序演算法排序演算法
- 歸併排序演算法(merge_Sort)排序演算法
- selection_Sort(選擇排序演算法)排序演算法
- Sorting 排序演算法: Quick Sort 快速排序排序演算法UI
- 經典排序演算法 - 快速排序Quick sort排序演算法UI
- How to draw a simple relation graph in PythonPython
- ARABIC-ENGLISH DICTIONARY: THE HANS WEHR DICTIONARY OF MODERN WRITTEN ARABIC
- 排序演算法(3)插入排序(Insertion Sort)排序演算法
- 排序演算法之「快速排序(Quick Sort) _c++ 」排序演算法UIC++
- 排序演算法之「插入排序(Insertion Sort)」排序演算法
- 排序演算法之「歸併排序(Merge Sort)」排序演算法
- 深入理解python中的排序sortPython排序
- Python中用來排序的方法sort、sortedPython排序
- dataGridView繫結Dictionary |Dictionary繫結到DataGridViewView
- How to Convert Decimal Numbers to Words with PythonDecimalPython