我想通過字典鍵排序詞典列表,我不想區分大寫和小寫字符。python:combine sort-key-functions itemgetter and str.lower
dict1 = {'name':'peter','phone':'12355'}
dict2 = {'name':'Paul','phone':'545435'}
dict3 = {'name':'klaus','phone':'55345'}
dict4 = {'name':'Krishna','phone':'12345'}
dict5 = {'name':'Ali','phone':'53453'}
dict6 = {'name':'Hans','phone':'765756'}
list_of_dicts = [dict1,dict2,dict3,dict4,dict5,dict6]
key_field = 'name'
list_of_dicts.sort(key=itemgetter(key_field))
# how to combine key=itemgetter(key_field) and key=str.lower?
for list_field in list_of_dicts:
print list_field[key_field]
應提供
Ali, Hans, klaus, Krishna, Paul, peter
,而不是
klaus, peter, Ali, Hans, Krishna, Paul
和作爲一個加號,它會自動與兩個字節串和unicode字符串工作。 – nosklo 2009-09-08 15:09:14