從這裏獲取解決方案:How to sort a list of lists by a specific index of the inner list?
from operator import itemgetter
L=[['University of Michigan','James Jones','phd'],['University of Georgia','Anne Greene','ba'],['University of Michigan','Frank Kimball','ma'],['University of Florida','Nate Franklin','ms'],['University of Georgia','Sara Dean','ms'],['University of Georgia','Beth Johnson','bs']]
print 'Before:', L
print ' After:', sorted(L, key=itemgetter(0))
輸出
Before: [['University of Michigan', 'James Jones', 'phd'], ['University of Georgia', 'Anne Greene', 'ba'], ['University of Michigan', 'Frank Kimball', 'ma'], ['University of Florida', 'Nate Franklin', 'ms'], ['University of Georgia', 'Sara Dean', 'ms'], ['University of Georgia', 'Beth Johnson', 'bs']]
After: [['University of Florida', 'Nate Franklin', 'ms'], ['University of Georgia', 'Anne Greene', 'ba'], ['University of Georgia', 'Sara Dean', 'ms'], ['University of Georgia', 'Beth Johnson', 'bs'], ['University of Michigan', 'James Jones', 'phd'], ['University of Michigan', 'Frank Kimball', 'ma']]
+1從u學到很多東西;) – zhangxaochen
@zhangxaochen歡迎您:)你善於numpy的自己:) – thefourtheye
待辦事項不要忘記,Python 2.6.6中不存在Counter,因此如果以後的Python不可用,就不能使用它。 – sabbahillel