python:组合sort-key-functions itemgetter和str.lower [英] python: combine sort-key-functions itemgetter and str.lower
问题描述
<$ 我想按字典键对字典列表进行排序,我不想区分大写字母和小写字符。 c $ c> dict1 = {'name':'peter','phone':'12355'}
dict2 = {'name':'Paul','phone':'545435'}
dict3 = {'name':'klaus','phone':'55345'}
dict4 = {'name':'Krishna','phone':'12345'}
dict5 = {'名字':'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))
#如何将key = itemgetter(key_field)和关键= str.lower?
for list_field in list_of_dicts:
print list_field [key_field]
应该提供
Ali,Hans,klaus,Krishna,Paul,peter
而不是
克劳斯,彼得,阿里,汉斯,克里希纳,保罗
在一般情况下,您需要编写用于分类目的的密钥提取功能;只有在特殊情况下(尽管很重要),您可以重新使用现有的调用方法来为您提取密钥,或者只需结合现有的一些调用(使用 lambda的快速和肮脏方式
,因为没有内置的函数组合方法)。
如果您经常需要执行这两种键提取操作(获取一个项目并调用该项目的方法),我建议:
def combiner(itemkey,methodname,* a, ** k):
def keyextractor(容器):
item = container [itemkey]
method = getattr(item,methodname)
返回方法(* a,** k )
return keyextractor
so listofdicts.sort(key = combiner ('name','lower'))
将适用于您的情况。
I want to sort a list of dictionaries by dictionary key, where I don't want to distinguish between upper and lower case characters.
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]
should provide
Ali, Hans, klaus, Krishna, Paul, peter
and not
klaus, peter, Ali, Hans, Krishna, Paul
In the general case, you'll want to write your key-extraction function for sorting purposes; only in special (though important) cases it happens that you can just reuse an existing callable to extract the keys for you, or just conjoin a couple of existing ones (in a "quick and dirty" way using lambda
, since there's no built-in way to do function composition).
If you often need to perform these two kinds of operations for key extraction (get an item and call a method on that item), I suggest:
def combiner(itemkey, methodname, *a, **k):
def keyextractor(container):
item = container[itemkey]
method = getattr(item, methodname)
return method(*a, **k)
return keyextractor
so listofdicts.sort(key=combiner('name', 'lower'))
will work in your case.
Note that while excessive generalization has costs, tasteful and moderate generalization (leaving the item key, method name, and method arguments if any, as runtime-determined, in this case) generally has benefits -- one general function, not more complex than a dozen specific and specialized ones (with the extractor, method to call, or both, hardwired in their code), will be easier to maintain (and, of course, much easier to reuse!-).
这篇关于python:组合sort-key-functions itemgetter和str.lower的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!